c++c++11g++decltypecompiler-bug

Is decltype(std) legal, and does it have any purpose?


When using decltype around a namespace, I can write code that compiles, but the statement doesn't seem to have any effect under g++4.9.1, under clang it produces error: unexpected namespace name 'std': expected expression

For example, the following all compile under g++, but the assembly doesn't show any code generated for them.

using s = decltype(std);
auto n = typeid(decltype(std)).name();
auto sz = n.size();
std::printf("size is %zu\n", sz+1);
std::printf("this type is: %s\n\n", n.c_str());

// the only limit is your imagination
int f();
std::ostream trash = f(typeid(decltype(std)) * 10 - 6 ^ typeid(decltype(std)));

If g++ is right in allowing this? If so, what's the advantage of the code disappearing rather than causing a compile-time error?


Solution

  • No, it's not legal. The two decltype-specifier forms that are allowed by the grammar are (N3690, §7.1.6.2/1):

    decltype-specifier:
    decltype ( expression )
    decltype ( auto )

    and a namespace name is not an expression.

    The paragraph I quoted is from a standard draft post C++11, so decltype(auto) doesn't apply to C++11. The answer is the same, though.