0

Consider the following demonstrative program.

#include <iostream>

namespace N
{
    struct A
    {
        static int n;
    };
    
    A A;
}

int N::A::n = 10;

int main() 
{
    std::cout << N::A::n << '\n';
    std::cout << N::decltype( N::A )::n << '\n';
    
    return 0;
}

The program compiles successfully using gcc 8.3 at for example www.ideone.com.

However if to run this program using MS VS 2019 then the compiler issues an error relative to the record decltype( N::A ) in the nested-name-specifier. If to remove preceding name N:: then the program compiles successfully.

Is it a bug of the MS VS compiler or is the nested-name-specifier written incorrectly?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • somewhat related: https://stackoverflow.com/questions/65077931/why-does-this-use-of-using-declaration-compile (Note that the question mentions MSVC accepting "similar but different" invalid code, which is rather the oposite of situation here, also OP doesnt mention what code that is.) – 463035818_is_not_an_ai May 07 '21 at 13:45
  • Also - why would you do this? I don't get the reasoning behind `N::decltype( N::A )::n`. `decltype( N::A )` already resolves to `N::A`, so I don't understand what prefixing it with `N::` would accomplish (GCC is definitely wrong to accept this - Clang also rejects this code) – mcilloni May 07 '21 at 20:27

1 Answers1

3

A decltype-specifier can never appear except at the beginning of a nested-name-specifier. After all, it designates a specific type, and no name lookup is necessary afterwards to interpret it. GCC is wrong to accept the code: by experimentation, it seems to just ignore any preceding components after checking that they exist.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76