模板别名非常方便地将typename F <T>::type这样的类型简化为F <T>,其中T和type是类型。
我也想对像F <T>::map这样的模板做同样的事情,也就是说,将它们简化为F <T>,其中T和map是模板结构或别名。
例如,考虑以下定义:
template <bool B>
using expr = std::integral_constant <bool, B>;
template <bool B>
using _not = expr <!B>;
template <template <typename> class F>
struct neg_f
{
template <typename T>
using map = _not <F <T>{}>;
};
template <typename T>
pred = expr < /* ... T ... */ >; // e.g., pred = expr <true>;
template <template <typename> class F>
struct fun;现在有以下工作:
fun <neg_f <pred>::map>这样做会方便得多,但却失败了:
template <template <typename> class F>
using neg = neg_f <F>::map;
fun <neg <pred> >( neg = neg_f <F>::template map也会失败,即使map被定义为一个结构)。以上neg的定义似乎更像是一个“模板别名”。
template <template <typename> class F>
template <typename T>
using neg = neg_f <F>::template map <T>;但显然不存在这样的事情。
那么,有什么解决方案吗?还是我应该继续使用neg_f <pred>::map?
发布于 2014-04-11 10:05:06
首先,考虑使用typename关键字来声明这是一个嵌套类型,而不管是类型(例如struct、class等)、模板类型、ty对联或别名。
别名规格说明要求您使用类型id来指定先前定义的类型。在这种情况下,正确使用type-id如下所示:
template< template<typename> class F, class T>
using neg_v2 = typename neg_f<F>::template map<T>;
// or
struct foo {};
template< template<typename> class F>
using neg_v1 = typename neg_f<F>::template map<foo>;您最初要做的是使用模板名neg_f<F>::map作为类型id。这不对。
您可能想从F中推断出F参数,以便在template map<T>中使用,但这不适用于最终用例fun<neg<pred>>,其中T未被解析。
https://stackoverflow.com/questions/18699973
复制相似问题