vendredi 3 septembre 2021

Why does default-constructibility behave weirdly for inner structs with NSDMI?

Consider the following code:

#include <type_traits>

struct outer {
    struct inner {
        unsigned int x = 0;
    };
//    static_assert(std::is_default_constructible<inner>::value,
//          "not default ctorable - inside");
};

static_assert(std::is_default_constructible<outer::inner>::value,
    "not default ctorable - outside");

This compiles fine. But - if I uncomment the static assert inside outer - both asserts fail with clang++ and gcc++. Why should they not both pass?

Notes:

  • If you remove the initializer of x and enable the assertions, the code compiles.

  • This:

    #include <type_traits>
    
    struct outer {
         struct inner {
             unsigned int x = 0;
         };
    
         inner get_an_inner() { 
             static_assert(std::is_default_constructible<outer::inner>::value,
                 "not default ctorable - outside");
             return inner{}; 
         }
    };
    

    compiles!

  • There's a LLVM bug report about basically the same thing, but it's not just clang and the bug report has not gotten any comments.

Aucun commentaire:

Enregistrer un commentaire