Detect if a default constructor exists at compile

2019-02-28 07:43发布

问题:

This question already has an answer here:

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

I'm trying to check if a default constructor exists for a template argument. I want to do something like this:

template <typename A>
class Blah
{
   Blah() { A* = new A(); } 
}

But i want to detect at compile time via SFINAE or some other trick if that constructor exists, and raise a static_assert of my own if it doesn't.

The problem arises when i have classes (like std::vector) that dont have a "default constructor" but a constructor with default parameters.

So using std::has_trivial_default_constructor won't return true. Although i can use new vector<T>().

回答1:

Here's a possible implementation of the type trait:

template<typename T>
class is_default_constructible {

    typedef char yes;
    typedef struct { char arr[2]; } no;

    template<typename U>
    static decltype(U(), yes()) test(int);

    template<typename>
    static no test(...);

public:

    static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};

struct foo {
    foo(int) {}
};

int main()
{
    std::cout << is_default_constructible<foo>::value << '\n'        // 0
              << is_default_constructible<std::vector<int>>::value;  // 1
}


回答2:

There is no std::has_trivial_default_constructor in the C++ standard; this appears to be a gcc enhancement that came from Boost. This is not what you want. You don't want to check if something has a trivial default constructor; you want to check if some class has a default constructor, trivial or not. Use std::is_default_constructible (assuming a c++11 compliant-compiler).