Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the number of members of a union

Tags:

c++

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

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 do I implement/use it?

like image 988
hermit.crab Avatar asked Mar 07 '23 16:03

hermit.crab


1 Answers

No, this is not possible in C++.

C++ does not have reflection, a feature for code that describes itself.

like image 188
Lightness Races in Orbit Avatar answered Mar 15 '23 11:03

Lightness Races in Orbit