C ++模板类型规范

问题描述:

我有一个与空白函数同名的空类.当我尝试将此类作为模板参数传递时,收到错误消息:

I have an empty class with the same name as a blank function. When I try to pass this class as a template parameter I receive an error:

"参数1处的类型/值不匹配"

"'Test'不是参数'_Ty'的有效模板类型自变量"

考虑:

#include <vector>

void Test() {
}

class Test {
};

int main() {
    std::vector<Test> test;
}

更改为

std::vector<class Test>

似乎可以正常工作,但是我无法确定这是标准要求还是编译器随机支持.

seems to work, but I was not able to found out, whether this is required by a standard, or just randomly supported by my compiler.

有人可以指出如何解决此问题或链接到需要这种行为的标准吗?

是的,您必须在名称前加上关键字class来消除歧义,这会产生复杂的类型说明符.

Yes you have to use the keyword class prepended to the name for disambiguation, which resulting in an elaborated type specifier.

[class.name]/2 :

(重点是我的)

如果在变量,函数或 还声明了同名的枚举数,然后 声明在范围内,只能使用 精致的类型说明符([basic.lookup.elab]). [示例:

If a class name is declared in a scope where a variable, function, or enumerator of the same name is also declared, then when both declarations are in scope, the class can be referred to only using an elaborated-type-specifier ([basic.lookup.elab]). [ Example:

struct stat {
  // ...
};

stat gstat;                     // use plain stat to define variable

int stat(struct stat*);         // redeclare stat as function

void f() {
  struct stat* ps;              // struct prefix needed to name struct stat
  stat(ps);                     // call stat()
}

-示例]

[dcl.type.elab] :

精心设计的类型说明符:

  • 类密钥属性说明符-seq opt 嵌套名称说明符 opt 标识符
  • class-key simple-template-id
  • 类键嵌套名称说明符模板 opt 简单模板ID
  • 枚举嵌套名称说明符 opt 标识符
  • class-key attribute-specifier-seqopt nested-name-specifieropt identifier
  • class-key simple-template-id
  • class-key nested-name-specifier templateoptsimple-template-id
  • enum nested-name-specifieropt identifier