1
struct Outer_t{
private:
    struct Inner_t {  
        int A;
        int B;
 
    };
    //const Inner_t NotStored = Inner_t{-1, -1};     <<-- works
    const auto NotStored = Inner_t{-1, -1};  <<-- error: 'auto' is not allowed here
};

Why is auto not allowed here? If I move Inner_t out of the enclosing struct, then it works fine.

struct Plain_t {  
    int A;
    int B;
 
};
//const Plain_t NotStored = Plain_t{-1, -1};     <<-- works
const auto NotStored = Plain_t{-1, -1};  <<-- now it works

For the record I'm using clang 13:

% clang --version
Homebrew clang version 13.0.1
Target: x86_64-apple-darwin21.5.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
Johan
  • 74,508
  • 24
  • 191
  • 319
  • what if you change the access specifier to `public`? – folibis Jul 03 '22 at 10:47
  • *"move `Inner_t` out of the enclosing struct"* You do more than this, you also move the variable out of the sturct. `auto` is not allowed on non-static data-members, regardless of their type. Presumably because you could have a different initializer type in the constructor (but IMO it would be ok to always deduce to the type of the in-class initializer...). – HolyBlackCat Jul 03 '22 at 10:47
  • @folibis tried: makes no difference. Anyway that detail is private for a reason. – Johan Jul 03 '22 at 10:51
  • Does not reproduce on clang 13.0.1 on compiler explorer : https://godbolt.org/z/sdoz3oTvP. – Pepijn Kramer Jul 03 '22 at 11:01
  • @PepijnKramer You did use the second code snippet in the question instead of the first one. The question does state that the second code snippet compiles. Actually using the first code snippet (with the `<<--` commented out) in godbolt does result in a compilation error. – fabian Jul 03 '22 at 11:05

0 Answers0