2

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?

Community
  • 1
  • 1
BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • if you had typed `A::v` , it'd have also saved you from more typing :D – Mr.Anubis Jan 28 '12 at 20:57
  • 1
    @Mr.Anubis: And if it were `std::map> m; auto a = m.begin();`.... then would writing the typename instead of `decltype(a)` be a savings? – Ben Voigt Jan 28 '12 at 21:00
  • @BenVoigt Not really a dupe, and I don't like the answer to that question. The answer to my question is in that question – BЈовић Jan 28 '12 at 21:01
  • @VJovic: Apart from the fact that his class is named `Foo` instead of `A`, and the static member is `i` instead of `v`, there's no difference at all. litb offered a second workaround, `std::identity`. And "the answer to my question is in that question" is also grounds for closure as a dupe. – Ben Voigt Jan 28 '12 at 21:19

2 Answers2

1

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

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • yes, I enabled c++0x std compilation. Oh well, I'll just use a typedef workaround for now – BЈовић Jan 28 '12 at 20:49
  • @BenVoigt: That the compiler is not recognizing the `decltype` keyword. – Puppy Jan 28 '12 at 21:47
  • your ideone link seems to have been removed (it just links to the start page). Any chance you remember what you typed in that one? – default Sep 28 '15 at 20:32
  • 1
    @Default: The question this is a dupe of shows how to use `typedef` as a workaround (and yes, ideone deleting code after they've promised to keep it available "Forever." is annoying) – Ben Voigt Sep 28 '15 at 20:36
1

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 ::.

Puppy
  • 144,682
  • 38
  • 256
  • 465