1

It is possible to check for a user defined constructor?

For example I have the following code

class A {};

class B {
public: 
    B(){/* do stuff */}
};

And I want to be able to do something like this:

printf("%d", check_for_user_default_constructor<B>::value);
printf("%d", check_for_user_default_constructor<A>::value);

and to have a true value only for class B.

tairqammar
  • 151
  • 3
  • 10
  • Would also https://stackoverflow.com/questions/257288/is-it-possible-to-write-a-template-to-check-for-a-functions-existence help? – Michael Chourdakis May 06 '19 at 13:04
  • 2
    What are you trying to accomplish? The difference between user-defined and compiler-defined seems somewhat useless in the field. – rubenvb May 06 '19 at 13:05
  • 1
    Why do you need to check for this? What is the underlying problem you're trying to solve? Or is this just plain curiosity? If not for curiosity, then please ask about your *actual* problem instead. – Some programmer dude May 06 '19 at 13:10
  • 6
    [std::is_trivial](https://en.cppreference.com/w/cpp/types/is_trivial) and [std::is_trivially_default_constructible](https://en.cppreference.com/w/cpp/types/is_trivially_default_constructible) come to mind. – dewaffled May 06 '19 at 13:10
  • 1
    IMO this is [XY problem](http://xyproblem.info/). – Marek R May 06 '19 at 13:21
  • 2
    This question might be a duplicate (idk), but the linked answers are not answering OPs question imo. The linked answers tell you how to check for a default constructor, but OP asks for a trait to check whether the default constructor is user defined or not. @dewaffled provided an actual answer with his comment. [Here](https://godbolt.org/z/pTuQGV) is an example usage. – Timo May 06 '19 at 13:24

0 Answers0