Detect if a default constructor exists at compile time [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 08:29:17

问题


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).



来源:https://stackoverflow.com/questions/13786482/detect-if-a-default-constructor-exists-at-compile-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!