使用 decltype 访问静态字段

发布于 2024-12-29 12:59:56 字数 576 浏览 3 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2025-01-05 12:59:56

编译器似乎无法识别 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

毁梦 2025-01-05 12:59:56

它是标准的,或者至少,它确实是标准的。我相信已经提交了一份关于此问题的 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 ::.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文