简体   繁体   English

C ++模板类型规范

[英]C++ template type specification

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: 当我尝试将此类作为模板参数传递时,收到错误消息:

" type/value mismatch at argument 1 " 参数1的类型/值不匹配

" 'Test' is not a valid template type argument for parameter '_Ty' " 'Test'不是参数'_Ty'的有效模板类型参数

Consider: 考虑:

#include <vector>

void Test() {
}

class Test {
};

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

Changing to 更改为

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. 似乎可行,但是无论是标准要求还是编译器随机支持,我都无法发现。

Can someone point out, how to solve this issue or link to standard, that requires this behavior? 有人可以指出如何解决此问题或链接到需要这种行为的标准吗?

Yes you have to use the keyword class prepended to the name for disambiguation, which resulting in an elaborated type specifier. 是的,您必须使用名称前面的关键字class来消除歧义,这会导致复杂的类型说明符。

[class.name]/2 : [class.name] / 2

(emphasis mine) (强调我的)

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]). 如果在范围内声明了类名,并且还声明了相同名称的变量,函数或枚举器,则当两个声明都在范围内时, 只能使用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() } 

— end example ] —结束示例]

And [dcl.type.elab] : [dcl.type.elab]

elaborated-type-specifier: 详细类型说明符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM