C++从类构造函数参数设置模板布尔参数

lzfw57am  于 2023-02-26  发布在  其他
关注(0)|答案(1)|浏览(144)

下面我已经尽可能地简化了这个场景,想象一个类使用bool参数进行模板特殊化。

template <bool hasExtraParam>
class TestClass {};

template <>
class TestClass<true> {
   public:
    int param1;
    int param2;
};

template <>
class TestClass<false> {
   public:
    int param1;
};

现在,我希望有一个容器类,它包含大量的这些TestClass作为成员变量。我希望能够根据构造函数参数设置每个成员变量的模板参数,如下所示:

constexpr bool ep1, ep2, ep3, ep4;
class Container
{
   public:
    constexpr Container(bool extraParam1, bool extraParam2, bool extraParam3,
                        bool extraParam4)
    {
        ep1 = extraParam1;
        ep2 = extraParam2;
        ep3 = extraParam3;
        ep4 = extraParam4;
    }
    TestClass<ep1> testClass1;
    TestClass<ep2> testClass2;
    TestClass<ep3> testClass3;
    TestClass<ep4> testClass4;
};

我怎样才能实现这个模式呢?我想让我的实际用例传入一个大的config结构体,它有一个布尔值,将链接到每个成员变量,设置其各自的模板参数。我不能绕着我的头如何实现这一点,感觉我错过了一些替代的基本方法的问题。此外,我不认为容器为了可伸缩性的目的而拥有一堆模板化的参数是可行的,因为配置结构可能很大。

k97glaaz

k97glaaz1#

您所尝试的操作在C++中是不可能的。模板参数只能在编译时指定。您必须在运行时动态示例化对象,例如:

class TestClassBase {
public:
    virtual ~TestClassBase() = default;

    // common members of both classes...

    // virtual methods for both classes to override...
};

template <bool hasExtraParam>
class TestClass {};

template <>
class TestClass<true> : public TestClassBase {
public:
    int param1;
    int param2;

    // override virtual methods as needed...
};

template <>
class TestClass<false> : public TestClassBase {
public:
    int param1;

    // override virtual methods as needed...
};

class Container
{
public:
    Container(bool extraParam1, bool extraParam2, bool extraParam3, bool extraParam4)
    {
        testClass1 = extraParam1 ? new TestClass<true> : new TestClass<false>;
        testClass2 = extraParam2 ? new TestClass<true> : new TestClass<false>;
        testClass3 = extraParam3 ? new TestClass<true> : new TestClass<false>;
        testClass4 = extraParam4 ? new TestClass<true> : new TestClass<false>;
    }

    ~Container()
    {
        delete testClass1;
        delete testClass2;
        delete testClass3;
        delete testClass4;
    }

    TestClassBase* testClass1;
    TestClassBase* testClass2;
    TestClassBase* testClass3;
    TestClassBase* testClass4;
};

或者:

template <bool hasExtraParam>
class TestClass {};

template <>
class TestClass<true> {
public:
    int param1;
    int param2;
};

template <>
class TestClass<false> {
public:
    int param1;
};

class Container
{
public:
    Container(bool extraParam1, bool extraParam2, bool extraParam3, bool extraParam4)
    {
        if (extraParam1)
            testClass1.emplace<TestClass<true>>();
        else
            testClass1.emplace<TestClass<false>>();

        if (extraParam2)
            testClass2.emplace<TestClass<true>>();
        else
            testClass2.emplace<TestClass<false>>();

        if (extraParam3)
            testClass3.emplace<TestClass<true>>();
        else
            testClass3.emplace<TestClass<false>>();

        if (extraParam4)
            testClass4.emplace<TestClass<true>>();
        else
            testClass4.emplace<TestClass<false>>();
    }

    std::variant<TestClass<true>, TestClass<false>> testClass1;
    std::variant<TestClass<true>, TestClass<false>> testClass2;
    std::variant<TestClass<true>, TestClass<false>> testClass3;
    std::variant<TestClass<true>, TestClass<false>> testClass4;
};

相关问题