【发布时间】:2017-11-17 09:06:13
【问题描述】:
考虑以下模板类的实现:
template<class T>
class MyClass
{
public:
void setVar1(const T& v1)
{
var1 = v1;
}
void setVar2(const T1& v2)
{
var2 = v2;
}
T var1;
T1 var2;
};
如果模板参数T 是基本类型(如float、double 或long double)我想要T1 = T。
如果模板参数T是std::complex<float>,我想要T=std::complex<float>和T1 = float。 std::complex<double> 和 std::complex<long double> 也是如此。
推导变量的类型在Template type derivation讨论
但是,附加的成员函数会阻止在此上下文中使用其解决方案。
【问题讨论】:
-
如果您只是在成员函数之前添加
using T1 = typename myTypeTraits<T>::type;,那么该解决方案应该可以工作。你发现了什么问题? -
一个专门针对模板-模板参数的内部(和私有)特征类似乎是一种解决方案。
-
A possible solution,我相信有更简单的方法
-
@BoPersson 感谢您的宝贵意见。在您的评论的帮助下,我已经发布了我的问题的答案。我还报告了一个与之相关的问题。你能看一下吗?
-
@BoPersson 我提到的问题现在已作为问题发布在stackoverflow.com/questions/47356284/…
标签: c++ c++11 templates template-argument-deduction