简体   繁体   中英

Does the using declaration allow for incomplete types in all cases?

I'm a bit confused about the implications of the using declaration. The keyword implies that a new type is merely declared. This would allow for incomplete types. However, in some cases it is also a definition, no? Compare the following code:

#include <variant>
#include <iostream>

struct box;

using val = std::variant<std::monostate, box, int, char>;

struct box
{
    int a;
    long b;
    double c;
    
    box(std::initializer_list<val>) {
        
    }
};

int main()
{
    std::cout << sizeof(val) << std::endl;
}

In this case I'm defining val to be some instantiation of variant. Is this undefined behaviour? If the using-declaration is in fact a declaration and not a definition, incomplete types such as box would be allowed to instantiate the variant type. However, if it is also a definition, it would be UB no?

For the record, both gcc and clang both create "32" as output.

Since you've not included , I'm attempting a non-lawyer answer.

Why should that be UB?

With a using delcaration, you're just providing a synonym for std::variant<whatever> . That doesn't require an instantiation of the object, nor of the class std::variant , pretty much like a function declaration with a parameter of that class doesn't require it:

void f(val); // just fine

The problem would occur as soon as you give to that function a definition (if val is still incomplete because box is still incomplete):

void f(val) {}

But it's enough just to change val to val& for allowing a definition,

void f(val&) {}

because the compiler doesn't need to know anything else of val than its name.


Furthermore, and here I'm really inventing, "incomplete type" means that some definition is lacking at the point it's needed, so I expect you should discover such an issue at compile/link time, and not by being hit by UB. As in, how can the compiler and linker even finish their job succesfully if a definition to do something wasn't found?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM