在C ++中使用标准库函数名称作为标识符是否有效? [英] is it valid to use standard library function name as identifier in C++?

查看:123
本文介绍了在C ++中使用标准库函数名称作为标识符是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下程序:

#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.

[C ++ 14:3.3.1/1]: [..] 要确定声明的范围,有时可以方便地引用声明的潜在范围.声明的范围与其潜在范围相同,除非潜在范围包含另一个同名声明.在这种情况下,内部(包含)声明性区域中声明的潜在范围被排除在外部(包含)声明性区域中声明的范围之外.

[C++14: 3.3.1/1]: [..] To determine the scope of a declaration, it is sometimes convenient to refer to the potential scope of a declaration. The scope of a declaration is the same as its potential scope unless the potential scope contains another declaration of the same name. In that case, the potential scope of the declaration in the inner (contained) declarative region is excluded from the scope of the declaration in the outer (containing) declarative region.

例如,您通常无法在名称空间范围内执行此操作

由于标准库中实体的名称不是固有保留的名称,因此定义明确:

It's well-defined because the names of entities in the standard library are not inherently reserved names:

[C ++ 14:2.11/3]:另外,某些标识符保留供C ++实现和标准库(17.6.4.3.2)使用,否则不得使用;无需诊断.

[C++14: 2.11/3]: In addition, some identifiers are reserved for use by C++ implementations and standard libraries (17.6.4.3.2) and shall not be used otherwise; no diagnostic is required.

[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 ++中使用标准库函数名称作为标识符是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆