Is there a way to test whether a C++ class has a default constructor (other than compiler-provided type traits)?

Sorry for answering may own question.

Googling I have found that the actual reason we can not check if a class has constructor or a destructors is that, the known technique used to detect if a class has a member is based on taking the address of the member. But constructors and destructors have no name, we can not take the address of them.

If we can not take the address, I don’t see a way to make the compiler react to a construction without instantiating it directly, but in this case there is no detection at compile time but an error.

So to answer my own question, I would say that with the current techniques it is not possible to detect them and compiler support is needed. But C++ has revealed a lot of surprises, and things that were not possible at a given time, were revealed are possible using another technique.

I hope a C++ language expert is reading that and can give a more clear explanation.

Leave a Comment