可变参数模板模板参数接受任何模板:
template<typename T>
struct Test1 {
using type = int;
};
template<typename T, typename T1>
struct Test2 {
using type = char*;
};
template<template<typename...S> class BeCurry>
struct Currying {
};
using curry = Currying<Test1>;
using curry2 = Currying<Test2>;
我想要Currying模板模板类
这意味着:如果参数接受一个模板参数Test1
,curry::apply<T>::type get Test1<T>::type
。如果参数接受两个模板参数Test2
,则curry2::apply<T0>
是“部分”模板,curry2::apply<T0>::apply<T1>::type get Test2<T0,T1>::type
这可以实现吗?因为我无法查询模板模板参数的内部参数num:
template<template<typename... S> class BeCurry>
struct Currying {
enum { value = sizeof...(S) }; // error!
};
答案 0 :(得分:3)
简单的解决方案是:
template
<
template <typename...> class BeCurry,
typename... Params
>
struct Currying
{
template <typename... OtherParams>
using curried = BeCurry<Params..., OtherParams...>;
template <typename... OtherParams>
using type = typename curried<OtherParams...>::type;
template <typename... NewParams>
using apply = Currying<curried, NewParams...>;
};
但由于编译错误(至少在 gcc 下),它不适用于Test1
和Test2
等模板。此问题的解决方法如下所示:
template
<
template <typename...> class BeCurry,
typename... Params
>
struct Curry
{
using type = BeCurry<Params...>;
};
template
<
template <typename...> class BeCurry
>
struct Curry<BeCurry>
{
using type = BeCurry<>;
};
现在行
template <typename... OtherParams>
using curried = BeCurry<Params..., OtherParams...>;
应替换为行
template <typename... OtherParams>
using curried = typename Curry<BeCurry, Params..., OtherParams...>::type;
使用示例:
#include <iostream>
#include <typeinfo>
template <typename T>
void print_type(T t)
{
std::cout << typeid(t).name() << std::endl;
}
// ...
print_type(Currying<Test1>::type<int>{});
print_type(Currying<Test1>::apply<int>::type<>{});
print_type(Currying<Test2>::type<int, char>{});
print_type(Currying<Test2>::apply<int>::type<char>{});
print_type(Currying<Test2>::apply<int>::apply<char>::type<>{});
print_type(Currying<Test2>::apply<int, char>::type<>{});
ideone的完整示例。
答案 1 :(得分:0)
在some problems of my own之后,我想出了这个解决方案,它适用于任何模板类(也就是你在帖子中提供的模板类)。
该解决方案的核心是is_valid_specialization
,用作判断currying过程是否完成的条件:
#include <iostream>
#include <type_traits>
template<template<typename...> class C, typename... T>
struct is_valid_specialization {
typedef struct { char _; } yes;
typedef struct { yes _[2]; } no;
template<template<typename...> class D>
static yes test(D<T...>*);
template<template<typename...> class D>
static no test(...);
constexpr static bool value = (sizeof(test<C>(0)) == sizeof(yes));
};
namespace detail {
template<template<typename...> class BeCurry, bool = false, typename... S>
struct Currying {
template<typename... T>
using apply = Currying<BeCurry, is_valid_specialization<BeCurry, S..., T...>::value, S..., T...>;
};
template<template<typename...> class BeCurry, typename... S>
struct Currying<BeCurry, true, S...> {
template<typename... T>
using apply = Currying<BeCurry, is_valid_specialization<BeCurry, S..., T...>::value, S..., T...>;
using type = typename BeCurry<S...>::type;
};
}
template<template<typename...> class BeCurry>
using Currying = detail::Currying<BeCurry, is_valid_specialization<BeCurry>::value>;
template<typename T>
struct Test1 { using type = int; };
template<typename T1, typename T2>
struct Test2 { using type = char*; };
template<typename...>
struct Test3 { using type = double; };
using curry = Currying<Test1>;
using curry2 = Currying<Test2>;
using curry3 = Currying<Test3>;
template<typename T>
void pretty_print(T) {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
int main() {
pretty_print(typename curry::apply<char>::type{});
pretty_print(typename curry2::apply<int>::apply<char>::type{});
pretty_print(typename curry3::type{});
}
上的输出