小编T. *_*ers的帖子

有没有办法从专门的构造函数调用模板构造函数?

假设我有这门课:

template <class T>
class Test
{
    Test(T* x);

    const T* const t;
    int i{0};
};
Run Code Online (Sandbox Code Playgroud)

我希望t始终使用以下内容进行初始化x

template <class T> Test<T>::Test(T* x) : t{x} {}
Run Code Online (Sandbox Code Playgroud)

我有两个专业:

template <> Test<Foo>::Test(Foo* x) : t{x} { i = 1; }
template <> Test<Bar>::Test(Bar* x) : t{x} { i = 2; }
Run Code Online (Sandbox Code Playgroud)

接下来,我用其他一些东西扩展该类,第一个(模板化)构造函数所做的不仅仅是设置t.

所有我想做的事情都是为了T = FooT = Bar

有什么方法可以从专门的构造函数中调用模板化构造函数吗?

//This does not work, since it will create a delegation cycle
template <> Test<Foo>::Test(Foo* x) …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

4
推荐指数
1
解决办法
466
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1