Getting the number of union members

Is there a way to get the number of union members in C ++? For instance:

union U
{
    int a;
    double b;
    char c;
};

int main()
{
    std::cout << std::union_members_count<U>::value << std::endl;  // prints 3
}

Of course, std::union_members_count<>is fictional.

If there is a way, how to implement / use it?

+4
source share
1 answer

No, this is not possible in C ++.

C ++ is not reflected, a function for code that describes itself.

+7
source

Source: https://habr.com/ru/post/1694810/


All Articles