gpt4 book ai didi

c++ - C 标准库可扩展性对 C++ 程序有多大影响?

转载 作者:IT老高 更新时间:2023-10-28 21:34:09 29 4
gpt4 key购买 nike

获取此代码:

int issuecode(int i)
{
return 2 * i;
}

int main(int argc, char **argv)
{
return issuecode(argc);
}

按照我的理解,如果编译为 C 程序,它将具有未定义的行为。我根据这些标准报价进行推理:

C99、7.26(或 C11、7.31)

The following names are grouped under individual headers for convenience. All external names described below are reserved no matter what headers are included by the program.

C99、7.26.2(或 C11、7.31.2)

Function names that begin with either is or to, and a lowercase letter may be added to the declarations in the <ctype.h> header.

C99、7.1.3(或 C11、7.1.3)

  1. Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

    [...]

    • All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.
  2. [...] If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

基于以上,我相信函数名issuecode实际上保留用于 <ctype.h> ,因此该程序在技术上具有 UB。

问题 0(健全性检查):我对标准的解读是否正确,而程序的行为在技术上是否未定义?

问题一:程序编译成C++代码会有UB吗?

我相信答案是“不”,因为从下面的引文中,我想说 C 的“ future 库方向”不是 C++ 标准库的一部分,但我不太确定。

C++11、21.7

  1. Tables 74, 75, 76, 77, 78, and 79 describe headers <cctype>, <cwctype>, <cstring>, <cwchar>, <cstdlib> (character conversions), and <cuchar>, respectively.

  2. The contents of these headers shall be the same as the Standard C Library headers <ctype.h>, <wctype.h>, <string.h>, <wchar.h>, and <stdlib.h> and the C Unicode TR header , respectively, with the following modifications:

“以下修改”均未提及附加的保留标识符。表 74 是函数名称的分类列表,如 isdigitisalnum .

C++11、C.2

1. This subclause summarizes the contents of the C++ standard library included from the Standard C library. It also summarizes the explicit changes in definitions, declarations, or behavior from the Standard C library noted in other subclauses (17.6.1.2, 18.2, 21.7).

7. The C++ standard library provides 209 standard functions from the C library, as shown in Table 153.

同样,表 153 是一个税收 list 。

问题 2: 假设我在问题 1 上错了,并且程序实际上在 C++ 中也有 UB,那么以下更改会对此有影响吗?

namespace foo {

int issuecode(int i)
{
return 2 * i;
}

}

using namespace foo;

int main(int argc, char **argv)
{
return issuecode(argc);
}

注意: 标准引用来自草案 N1256 (C99)、N1570 (C11) 和 N3242 (C++11),它们是各自语言版本的最新公开可用草案。

最佳答案

The following names are grouped under individual headers for convenience. All external names described below are reserved no matter what headers are included by the program.

有一个预定义的保留函数列表,如果您的函数在名称上没有冲突,则没有问题。

Function names that begin with either is or to, and a lowercase letter may be added to the declarations in the <ctype.h> header.

操作术语可以添加到<ctype.h>标题。 “是或至”位只是对声明组织的指导。

所以那里从来没有真正的未定义行为......

至于 C++,我认为这遵循相同的想法,例如:

namespace foo{
int isupper ( int c );
}

#include <cctype>
using namespace foo;
int main(void){
isupper(92);
}

这应该会产生编译器错误,因为您的函数与 C 函数的名称冲突,但由于命名空间,这很容易通过附加 std:: 来解决。或 foo::到通话开始。

关于c++ - C 标准库可扩展性对 C++ 程序有多大影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17504565/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com