Accessing static fields by using decltype [duplicate]
Вопрос
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?
Решение
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
Другие советы
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 ::
.