C语言有一个很有趣的技巧,一个名称可以用来声明类型和函数:
struct foo {};
void foo(void);
现在,每当我写
foo
时,编译器会默认我指的是函数,除非我在前面加上struct
前缀:foo(); // call void foo(void)
struct foo bar; // declare variable of type struct foo
这在C++中非常好用,甚至可以处理命名空间和嵌套!但是它无法与模板一起使用:
template <typename> void bar(void);
template <typename> struct bar {};
// error: conflicting declaration of template struct bar
// previous declaration: template void bar()
为什么?
我们已经在依赖上下文中处理歧义消解器:
typename foo<A>::b();
foo<A>::template b<C>();
将常规规则应用于这种模板名称的歧义似乎并不困难。
假设对我而言拥有相同的名称很重要,我能否以某种方式使其起作用?
language-lawyer
标签,并且可能需要交叉引用C标准以获取细微差别和相似之处。 - Eljay