【发布时间】:2016-01-22 03:37:04
【问题描述】:
我有一个班级和一个班级模板
class promise;
template <class... Ts>
class typed_promise;
我打算设计一个转换实用程序在它们之间进行转换
template <class... Ts>
typed_promise<Ts...> promise_cast(promise x);
template <class... Ts>
promise promise_cast(typed_promise<Ts...> x);
所以,你可以做下面的演员表
promise pms;
typed_promise<int, int> typed_pms;
auto typed_pms2 = promise_cast<int, int>(pms);
auto pms2 = promise_cast(typed_pms);
我也想支持这样的用法
auto typed_pms2 = promise_cast<typed_promise<int, int>>(pms);
相当于
auto typed_pms2 = promise_cast<int, int>(pms);
既然C++不允许偏函数模板特化,那有没有可能实现我想要的呢?如果可能,我应该怎么做?
【问题讨论】:
-
“不允许”是错误的。 “不直接支持”更像是这样。但是我在这里没有看到 技术 问题,你有两个不同的重载。只需将纯声明更改为定义,the code works。
-
投票关闭,因为缺少问题示例。
-
@Cheersandhth.-Alf 那会是
typed_promise<typed_promise<int, int>>。 -
这在某种程度上取决于您需要做什么。您是否需要在
promise_cast实现中访问Ts...? -
如果
promise是typed_promise的基础,则转换问题将通过自然向上转换和static_cast来解决
标签: c++ c++11 template-meta-programming template-specialization overloading