问题描述
考虑以下程序:
#include <cstdio>
int main()
{
int printf=9;
std::printf("%d",printf);
}
使用内置函数名称作为变量声明中的标识符是否可以?这个程序定义明确吗?我的意思是上述程序的行为定义得很好吗?我很好奇C ++标准是否允许使用标准函数名作为变量的标识符
Is it fine to use built in function name as an identifier in variable declaration? Is this well defined program? I mean is behaviour of above program well defined? I am curious to know whether the C++ standard allows to use standard function names as identifiers of variables
推荐答案
格式正确,因为 std :: printf
和 :: printf
(也可能已经由< cstdio>
!声明了!)在与整数相同的作用域中声明,因此,在该块的持续时间内,此优先级会自动获得.
It's well-formed because neither std::printf
nor ::printf
(which may also have been declared by <cstdio>
!) are declared in the same scope as your integer, which therefore takes automatic precedence for the duration of the block.
由于标准库中实体的名称不是固有保留的名称,因此定义明确:
It's well-defined because the names of entities in the standard library are not inherently reserved names:
[C ++ 14:17.6.4.3.2/1]:
某些名称和函数签名集始终保留给实现:
[C++14: 17.6.4.3.2/1]:
Certain sets of names and function signatures are always reserved to the implementation:
- 每个包含双下划线
_ _
的名称或以下划线后跟大写字母(2.12)开头的名称将保留给实现以供任何使用. - 以下划线开头的每个名称都保留给实现,以用作全局名称空间中的名称.
- Each name that contains a double underscore
_ _
or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use. - Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.
这篇关于在C ++中使用标准库函数名称作为标识符是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!