【发布时间】:2020-08-29 06:35:47
【问题描述】:
typename 声明中的关键字在其后需要一个限定的类型名称(一个在其前面带有嵌套名称分隔符的类型名称)。但是本地类或结构的限定名称是什么?示例如下:
struct A{};
namespace bb { struct B{}; }
int main()
{
struct C{};
typename A a; // not OK, because A is not a qualified typename
typename ::A a; // OK, because it contains a nested name specifier
typename B b; // not OK, because B is not a qualified typename
typename bb::B b; // OK, because it contains a nested name specifier
typename C c; // not OK in gcc and clang but compiles in Visual Studio
}
这是我从 clang 得到的错误:expected a qualified name after 'typename'
这是gcc报错:<source>:21:14: error: expected nested-name-specifier before 'C'
可以在typename 之后使用本地类名吗?如果是这样,那么它的限定名称是什么?该类在 clang 错误消息中报告为 main()::C,但这显然不是它的限定名称。我在 C++ 标准中找不到禁止在这种情况下使用本地类的地方。我错过了什么吗? Visual Studio 没有抱怨 typename C c; 那么它是 gcc 和 clang 中的错误吗?
附言。我知道如果我不使用模板,我可以在变量前面声明没有 typename 的变量,但我只是好奇这是语言中的错误、gcc/clang 或 Visual Studio 中的错误,还是我遗漏了一些东西。
【问题讨论】:
-
"是否可以在 typename 之后使用本地类名"。为什么?
-
@Klaus 正如我在问题中所写,我出于好奇而问这个问题
-
寻找细节有时就像敲开C++地狱的后门:-)
-
已知 VS 不是很符合。
标签: c++ c++11 language-lawyer