你能用数字开始一个类名吗? [英] Can you start a class name with a numeric digit?
问题描述
在 C ++ 中,是否可以使用数字启动类名称?例如,
模板< class T> class 2DArray {
public:
// 1D ARRAY CLASS
class 1DArray {
public:
1DArray(){Create(); }
1DArray(iterator arr):array1d_(arr){}
explicit 1DArray(size_type cols,const T& t = T()){Create(cols,t); }
1DArray(const 1DArray& arr){Create(arr.begin(),arr.end()); }
1DArray& operator =(const 2DArray&);
〜1DArray(){Uncreate(); }
T& operator [](size_type n){
return array1d_ [n];
}
const T& operator [](size_type n)const {
return array1d_ [n];
}
}
中的标识符名称是:
- 不能以数字开头
- 可以由字母,数字,下划线,通用字符名称 1 和实现定义的字符组成
- a 关键字。
标识符:
标识符非数字< - 只能使用非数字
标识符标识,非数字<启动; - 接下来的两个规则,可以将后续
标识符数字< - 人物是那些上面
标识符非数字2所示:
非数字< - 包括AZ,az和_
基于通用字符名
其他实现定义的字符
[...]
和 2.12
关键字说明保留用作关键字的所有标识符。
最后,还保留了以下名称:
- 包含双下划线的名称
__
,或在任何范围内以下划线和大写字母(例如_Apple
)开头, - 与全局命名空间(在全局命名空间像
_apple
)下划线开头的名称被保留。
标准草案中的部分是 17.6.4.3.2
。我们可以找到为什么这些从理由保留国际准则的理由编程语言-C 它说:
[...]这给出了一个名字空间写了无数幕后...
脚注
- 1。允许的通用字符
允许的通用字符包含在 / code>:
E.1允许使用的字符范围[charname.allowed]
<00> 00A8,00AA,00AD,
<00> 00AF,00B2-00B5,00B7-00BA,00BC-00BE,00C0-00D6,00D8-00F6,00F8 -00FF
0100-167F,1681-180D,180F-1FFF 200B-200D,202A-202E,203F-2040,2054,
2060-206F 2070-218F,2460-24FF,2776-2793,2C00-2DFF,2E80-2FFF
3004-3007,3021- 302F,3031-303F
3040-D7FF F900-FD3D,FD40-FDCF,
FDF0- FE47-FFFD
10000-1FFFD,20000-2FFFD,30000-3FFFD,
40000-4FFFD,50000-5FFFD,60000-6FFFD,70000-7FFFD, 80000-8FFFD,
90000-9FFFD,A0000-AFFFD,B0000-BFFFD,C0000-CFFFD,D0000-DFFFD,
E0000-EFFFD
In C++, is it possible to start a class name with a digit? For example,
template <class T> class 2DArray {
public:
// 1D ARRAY CLASS
class 1DArray {
public:
1DArray() { Create(); }
1DArray(iterator arr) : array1d_(arr) { }
explicit 1DArray(size_type cols, const T& t = T()) { Create(cols, t); }
1DArray(const 1DArray& arr) { Create(arr.begin(), arr.end()); }
1DArray& operator=(const 2DArray&);
~1DArray() { Uncreate(); }
T& operator[](size_type n) {
return array1d_[n];
}
const T& operator[](size_type n) const {
return array1d_[n];
}
}
Rules for identifier names in C++ are:
- It can not start with a number
- Can be composed of letters, numbers, underscore, universal character names1 and implementation defined characters
- Can not be a keyword.
The sections in the C++ draft standard that cover this are 2.11
Identifiers which includes the following grammar:
identifier:
identifier-nondigit <- Can only start with a non-digit
identifier identifier-nondigit <- Next two rules allows for subsequent
identifier digit <- characters to be those outlined in 2 above
identifier-nondigit:
nondigit <- a-z, A-Z and _
universal-character-name
other implementation-defined characters
[...]
and 2.12
Keywords explains all the identifier reserved for use as keywords.
Finally, the following names are also reserved:
- Names that contain a double underscore
__
, or start with either an underscore followed by an uppercase letter (like_Apple
) in any scope, - Names that start with an underscore in the global namespace (like
_apple
in the global namespace) are reserved.
The section that covers this in the draft standard is 17.6.4.3.2
. We can find a rationale for why these are reserved from Rationale for International Standard—Programming Languages—C which says:
[...]This gives a name space for writing the numerous behind-the-scenes non-external macros and functions a library needs to do its job properly[...]
In C++ this also applies to name mangling as this example shows.
Footnotes
- 1. Allowed universal characters
The universal characters that are allowed is covered in Annex E.1
:
E.1 Ranges of characters allowed [charname.allowed]
00A8, 00AA, 00AD,
00AF, 00B2-00B5, 00B7-00BA, 00BC-00BE, 00C0-00D6, 00D8-00F6, 00F8-00FF
0100-167F, 1681-180D, 180F-1FFF 200B-200D, 202A-202E, 203F-2040, 2054,
2060-206F 2070-218F, 2460-24FF, 2776-2793, 2C00-2DFF, 2E80-2FFF
3004-3007, 3021-302F, 3031-303F
3040-D7FF F900-FD3D, FD40-FDCF,
FDF0-FE44, FE47-FFFD
10000-1FFFD, 20000-2FFFD, 30000-3FFFD, 40000-4FFFD, 50000-5FFFD, 60000-6FFFD, 70000-7FFFD, 80000-8FFFD, 90000-9FFFD, A0000-AFFFD, B0000-BFFFD, C0000-CFFFD, D0000-DFFFD, E0000-EFFFD
这篇关于你能用数字开始一个类名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!