假设我有这门课:
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 = Foo和T = Bar。
有什么方法可以从专门的构造函数中调用模板化构造函数吗?
//This does not work, since it will create a delegation cycle
template <> Test<Foo>::Test(Foo* x) …Run Code Online (Sandbox Code Playgroud)