gpt4 book ai didi

C++ 模板类型规范

转载 作者:行者123 更新时间:2023-11-30 01:37:03 24 4
gpt4 key购买 nike

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

参数 1 的类型/值不匹配

‘Test’不是参数‘_Ty’的有效模板类型参数

考虑:

#include <vector>

void Test() {
}

class Test {
};

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

更改为

std::vector<class Test>

似乎可行,但我无法确定这是标准要求的,还是我的编译器随机支持的。

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

最佳答案

是的,您必须在名称前使用关键字 class 来消除歧义,从而产生详细的类型说明符。

[class.name]/2 :

(强调我的)

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()
}

— end example ]

[dcl.type.elab] :

elaborated-type-specifier:

  • class-key attribute-specifier-seqopt nested-name-specifieropt identifier
  • class-key simple-template-id
  • class-key nested-name-specifier templateopt simple-template-id
  • enum nested-name-specifieropt identifier

关于C++ 模板类型规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50436381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com