您的解决方案不“符合标准”,因为成员函数不能专门用于类模板。这是因为函数不能被部分特化的一般规则 - 所以即使成员函数模板的“完全”特化实际上也是部分特化,因为不是完全特化的类。
我的解决方案:
C++11 版本
您以我的版本为例,我相信这就是您想要的:
int main(){
auto f1 = [](int i){return i*2.54;};
auto f2 = [](int i){ std::stringstream ss; ss << i; return ss.str(); };
MultiUnitValue<int, float, std::string> vv(1, f1, f2);
std::cout << vv.in<int>() << "\n";
std::cout << vv.in<float>() << "\n";
std::cout << vv.in<std::string>() << "\n";
// std::cout << vv.in<long>() << "\n"; // error to compile
}
首先 - 您需要特殊的转换基类,对于单个转换,您将在下一个代码片段中看到通过基类函数调用转换会导致“未指定”转换,像这样的 forlong
不会编译。
template <class T, class U>
class Conversion {
public:
Conversion(const std::function<U(const T&)>& f) : f(f) {}
U convert (const T& v) const { return f(v); }
private:
std::function<U(const T&)> f;
};
template <class T>
class Conversion<T,T> {
public:
T convert (const T& v) const { return v; }
};
你的班级使用可变参数模板:
template <class T, class... V> // V... means all desired conversions
class MultiUnitValue : private Conversion<T,T>, private Conversion<T,V>... {
// allowed conversion: ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
public:
MultiUnitValue(T v, const std::function<V(const T&)>&... f) : Conversion<T,V>(f)..., v(v) {}
template <class U>
U in() const
{
// this static assert is not needed - but just to show the message
static_assert(std::is_base_of<Conversion<T,U>, MultiUnitValue<T,V...>>::value,
"Not allowed conversion");
// static_assert is not needed
// since if you MultiUnitValue does not derive from Conversion<T,U>
// - then this call will not compile too
return this->Conversion<T,U>::convert(v);
}
private:
T v;
};
LVS 示例: http: //liveworkspace.org/code/05b6ada146cc8f05d027a5536859a087
没有可变参数模板的版本:
我还准备了没有可变参数模板的解决方案,因为 VC++ 仍然不支持它们。
第二:转换和覆盖限制现在应该在您的 T_to_U 类型中。
与 C++11 版本相比,使用这种方法会稍微不方便:
int main(){
auto f1 = [](int i){return i*2.54;};
auto f2 = [](int i){ std::stringstream ss; ss << i; return ss.str(); };
// next 2 lines differ from C++11 version
typedef ConvertFunctions2<int, float, std::string> CF_f1_f2;
MultiUnitValue<int, CF_f1_f2> vv(1, CF_f1_f2(f1, f2));
std::cout << vv.in<int>() << "\n";
std::cout << vv.in<float>() << "\n";
std::cout << vv.in<std::string>() << "\n";
// std::cout << vv.in<long>() << "\n"; // error to compile
}
MultiUnitValue
将比您的示例更简单,甚至在我的 C++11 版本中也更简单,但会class CF
复杂得多:
template <class T, class CF>
class MultiUnitValue {
public:
MultiUnitValue(T v, const CF& cf) : v(v), cf(cf) {}
template <class U>
U in() const
{
return cf.Conversion<T,U>::convert(v);
}
private:
T v;
CF cf;
};
简单的“帮助”转换类将与 C++11 版本中的相同:
template <class T, class U>
class Conversion {
...
};
template <class T>
class Conversion<T,T> {
...
};
以及 VC++ 中的可变参数模板替代方案(以及 C++03 的旧时代):
template <class T>
class ConvertFunctions0 : public Conversion<T,T> {};
template <class T, class V1>
class ConvertFunctions1 : public Conversion<T,T>, public Conversion<T,V1> {
public:
ConvertFunctions1(std::function<V1(const T&)> f1) : Conversion<T,V1>(f1) {}
};
template <class T, class V1, class V2>
class ConvertFunctions2 : public Conversion<T,T>, public Conversion<T,V1>, public Conversion<T,V2> {
public:
ConvertFunctions2(std::function<V1(const T&)> f1, std::function<V2(const T&)> f2)
: Conversion<T,V1>(f1), Conversion<T,V2>(f2)
{}
};
正如你所看到的 - 添加ConvertFunctions3
,ConvertFunctions4
不是那么大的麻烦......
ideone的完整示例