Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

Traits classes can be defined to check if a C++ class has a member variable, function or a type (see here).

Curiously, the ConceptTraits do not include traits to check if a C++ class defines a default constructor or given constructor?

Can traits be used to check the constructor presence? If yes, how? If not, why it is not possible?

like image 452
Vicente Botet Escriba Avatar asked Apr 28 '10 21:04

Vicente Botet Escriba


1 Answers

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.

like image 99
Vicente Botet Escriba Avatar answered Oct 02 '22 14:10

Vicente Botet Escriba