c++ - < : syntax in C? 的意义是什么

标签 c++ c gcc

<分区>

Possible Duplicate:
<: cannot begin a template argument list

你知道吗

int a<:10:>;

相当于

int a[10];

?

我正在编写一些代码,其中我有一个全局命名空间和一个受限命名空间,现在假设为 NS1。我的全局命名空间中有一个名为 Module 的类,我在 NS1 中导入了一些其他库,它们也有一个名为 Module 的类。我试图创建我的模块的 std::list,即 NS1 中的函数内的::Module 并这样做,我得到了这个编译错误

std::list<::Module*> &myModule;

genllvm.cpp:60:11: error: ‘<::’ cannot begin a template-argument list
./genllvm.cpp:60:11: note: ‘<:’ is an alternate spelling for ‘[’. Insert whitespace between ‘<’ and ‘::’
./genllvm.cpp:60:11: note: (if you use ‘-fpermissive’ G++ 

这个“<:”语法的意义是什么?

最佳答案

它调用替代标记。 C++ 有几个:

 <%     {
 %>     }
 <:     [
 :>     ]
 %:     #
 %:%:   ##
 and    &&
 bitor  |
 or     ||
 xor    ˆ
 compl  ~
 bitand &
 and_eq &=
 or_eq  |=
 xor_eq ˆ=
 not    !
 not_eq !=

您可以看到一些由字母组成的替代 token 。所以你可以写if (a<b and b<c)在可以正确处理它们的编译器中。它们的存在是因为键盘或字符集中缺少符号。替代标记永远不会被主要标记替换(与三字母不同),但它们的行为与主要标记相同。

但是,C++0x 需要对 <:: 进行特殊处理(2.5p3):

Otherwise, if the next three characters are <:: and the subsequent character is neither : nor >, the < is treated as a preprocessor token by itself and not as the first character of the alternative token <:.

所以SomeTemplate<::SomeClass>可以正确处理。

关于c++ - < : syntax in C? 的意义是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8338121/

相关文章:

c++ - llvm 以 char * 作为参数调用外部函数

c - c中 'or'逻辑之间的区别

c++ - Visual C++ 编译和生成巨大目标文件的嵌套 lambda 表达式非常慢

c++ - unique_ptr 和 shared_ptr 的区别

在 C 中使用指针将大写字母转换为小写字母

c - 我如何在 J 中编写此 C 表达式?

c - 为什么我的程序返回(空)和乱码而不是预期的输出

gcc - 为 arm32 构建内核时对 __aeabi_ldivmod 的 undefined reference

linux - 构建 Linux Kernel 4.18 时出现编译时错误

python - Pybind11 : "ImportError: DLL not found" when trying to import *. Python解释器中的pyd