Why is there not an std::is_struct type trait?

It returns true for both classes and structs. I know that in C++ they are almost the same thing, but I’d like to know why there’s not a distinction between them in the type trait.

Unfortunately this is a common misconception in C++. Sometimes it comes from fundamental misunderstanding, but at other times it comes from an ambiguity in English. It can come from inaccurate compiler diagnostics, badly-written books, incorrect SO answers…

You’ve probably read something like this:

“There is no difference in C++ between a struct and a class except the default visibility of members and bases.”

This passage can be interpreted in a sense that is misleading, because the notions of identity and equality are hard to distinguish when using phrases like “no difference”.

In fact, C++ has not had structs since 1985. It only has classes.

The kind of types that you declare with the keyword class and the keyword struct are classes. Period. The keyword struct, and the visibility rules that are the default when defining a class using that keyword, were kept only for backward compatibility with C … but that’s a syntax thing. It doesn’t make the resulting types actually be of a different kind.

The type trait makes no distinction because there literally isn’t one to make.

Leave a Comment