本文介绍了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.
(重点是我的)
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 嵌套名称说明符标识符
- class-key simple-template-id
- 类键嵌套名称说明符模板 简单模板ID
- 枚举嵌套名称说明符标识符
- class-key attribute-specifier-seq nested-name-specifier identifier
- class-key simple-template-id
- class-key nested-name-specifier template simple-template-id
- enum nested-name-specifier identifier
这篇关于C ++模板类型规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!