我如何有条件地定义默认构造函数?

问题描述 投票:7回答:7

我当时在考虑一个类似的课程:

template < typename ...Whatever >
class MyClass
{
public:
    static constexpr bool has_default_ctr = Something;

    // I want this only if "has_default_ctr" is "true".
    MyClass();

    //...
};

我认为我不能为此使用构造器模板和std::enable_if(因为没有参数)。我错了吗?如果没有,还有其他方法吗?

c++ c++11 default-constructor enable-if
7个回答
13
投票

C ++ 11允许(可靠地)在模板参数中使用enable_if样式的SFINAE:

template<
    // This is needed to make the condition dependent
    bool B = has_default_ctr
    , typename std::enable_if<B, int>::type = 0
>
MyClass();

// When outside of class scope:
// have to repeat the condition for out-of-line definition
template<bool B, typename std::enable_if<B, int>::type = 0>
MyClass::MyClass()
/* define here */

在C ++ 03中,您可以使用带有默认参数的一元构造函数-默认参数意味着该构造函数仍算作默认构造函数。


2
投票

由于注释中提到了带有默认参数的解决方案,但是我需要花一些时间来弄清楚该如何做(将我的Enabler类设为私有,这是行不通的),如果有人这样做,这里是建议的解决方案寻找它:

class MyClass {
public:
   // if constructor is supposed to be public,
   // this helper class must be public as well!
   struct Enabler {}; 

   template <class U = Enabler>
   MyClass (std::enable_if_t<has_default_ctr, U> = Enabler {})
   {
      // whatever
   }
};

1
投票

您可以做一些简单的事情

template < typename ...Whatever >
class MyClass
{
public:
    static constexpr bool has_default_ctr = Something;

    // I want this only if "has_default_ctr" is "true".
    MyClass()
    {
        static_assert(has_default_ctr, "Not Default Constructible");
    }

    //...
};

0
投票

要根据某些条件为类获得不同的定义,请将依赖性计算放在模板参数中。

// primary template, no default constructor unless Something is true
template< typename T, bool has_default_ctr = Something > class MyClass {
    // as you had it, with no default constructor
};

// you want MyClass<T,true> to be just like MyClass<T,false>
// but with a default constructor:
template< typename T > class MyClass<T,true> : public MyClass<T,false> {

    MyClass() : MyClass<T,false>(/* chosen constructor args */) { etc; }

    using MyClass<T,false>::MyClass<T,false>;
};

如果没有C ++ 11,则不能使用using构造函数继承,而必须重新声明其所有构造函数并将其参数转发给基类。

这是从键盘到键盘,我没有方便使用的编译器atm,因此可能存在较小的语法错误。


0
投票

这是我最近使用的那个。如果其数据成员支持默认构造函数,则我需要一个带有默认构造函数的类模板,否则默认编译器不应编译。

类模板定义了模板类型参数,而类定义了这种类型的数据成员(使用继承的变体也可能很有吸引力,因为它可以使用空的基类优化)。数据成员的默认构造函数由类的默认构造函数自动调用。如果数据成员的类型没有默认构造函数,则编译将失败。否则成功。这是代码:

#include <string>
template <typename Data>
class Demo
{
    Data d_data;

    public:
        Demo() = default;        // OK when used and Data has a default
                                 // constructor. 

        Demo(Data const &data)   // Exampe of an additional constructor
        :
            d_data(data)
        {}

        // ...
};

struct WithoutDefault
{
    WithoutDefault(int)
    {}
};

int main()
{
    Demo<std::string> withString;
    // Demo<NoDefault> nope;              // won't compile

    WithoutDefault nod(10);
    Demo<WithoutDefault> nodefault(nod);  // OK
}

0
投票

使用enable_if很简单(如果类也以模板形式也可以使用:

#include <type_traits>

class MyClass
{
public:
    static constexpr bool has_default_ctr = Something;

    // I want this only if "has_default_ctr" is "true".
    template <typename = std::enable_if<has_default_ctr>::type>
    MyClass();

    //...
};

-1
投票

您可以使用具有不同参数的不同构造函数

MyClass(){

}
MyClass(int num){

}
MyClass(String s){
}

您可以通过简单的write和static函数返回该类并在其中写入条件:

static chooseContructor(/*parameters*/){
 if(/*something*/){
     return new MyCLass();
 }
 else if(/*something else*/){
     return new MyClass(int num);
 }
 else if{
    return new MyClass(String s);
 }
}

依此类推...这样的东西会给你一个半自动的构造函数选择器]

© www.soinside.com 2019 - 2024. All rights reserved.