使用 decltype 访问静态字段
可能的重复:
C++0x decltype 和作用域解析运算符
使用以下命令编译下一个示例g++ 4.6.1:
#include <iostream>
struct A
{
static const int v = 1;
};
int main()
{
A a;
std::cout << decltype(a)::v << std::endl;
}
将产生下一个编译错误:
error: expected primary-expression before 'decltype'
error: expected ';' before 'decltype'
这符合标准吗?或者,这是 g++ 的怪癖吗?
Possible Duplicate:
C++0x decltype and the scope resolution operator
Compiling next example using g++ 4.6.1:
#include <iostream>
struct A
{
static const int v = 1;
};
int main()
{
A a;
std::cout << decltype(a)::v << std::endl;
}
will produce next compiling errors:
error: expected primary-expression before 'decltype'
error: expected ';' before 'decltype'
Is this according to the standard? Or, is it a g++'s quirk?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编译器似乎无法识别
decltype
关键字。G++ 4.6.1 足够新,包含了 decltype 关键字。您是否使用
-std=gnu++0x
或-std=c++0x
启用了 C++11 模式?C++ 语法确实允许 decltype-specifier 出现在 qualified-id 中的
::
之前,因此代码将被符合要求的代码接受。编译器。错误消息是错误的,decltype(a)::v
是一个有效的qualified-id,它是一个primary-expression。作为解决方法,您可以使用 typedef。示例:http://ideone.com/clone/7FKUJ
It looks as if the compiler isn't recognizing the
decltype
keyword.G++ 4.6.1 is new enough to include the
decltype
keyword. Did you enable C++11 mode with-std=gnu++0x
or-std=c++0x
?The C++ grammar does permit a decltype-specifier to appear before
::
in a qualified-id, so the code will be accepted by a conforming compiler. The error message is wrong,decltype(a)::v
is a valid qualified-id, which is a primary-expression.As a workaround, you can use a typedef. Example: http://ideone.com/clone/7FKUJ
它是标准的,或者至少,它确实是标准的。我相信已经提交了一份关于此问题的 DR,并且它可能已在最终标准中得到修复,但可能需要在下一个标准中进行修复。很简单,
decltype
不是::
之前的有效语法产生式。It is Standard, or at least, it certainly was. I believe that there was a DR filed about this, and it might have been fixed in the final Standard but it might be due for a fix in the next Standard. It is as simple as that a
decltype
is not a valid grammatical production before::
.