假设您有一个元组类型,您希望提取它的模板参数包,以便实例化另一个模板。如果这是一个类型模板,那么我可以拥有这样的实用程序:
template < typename Tuple, template <typename...> typename What >
struct PutTupleInT;
template < typename... Types, template <typename...> typename What >
struct PutTupleInT<std::tuple<Types...>, What>
{
using Result = What<Types...>;
};但是,如果所需的模板是可变模板呢?虽然template <typename...> typename What是类型模板的“占位符”,但是变量模板的“占位符”是什么呢?
我已经为clang-4.0.0 (到目前为止唯一支持自动类型的非类型模板参数的编译器)尝试了以下操作,但是失败了。实际上,我不确定这是否是C++17的正确语法。
template < typename Tuple, template <typename...> auto What >
struct PutTupleInV;
template < typename... Types, template <typename...> auto What >
struct PutTupleInV<std::tuple<Types...>, What>
{
static constexpr auto value = What<Types...>;
};发布于 2016-10-23 09:20:33
我不认为你能做到。引用N4606:
§14.3.3 tem.arg.Template/1 模板模板参数的模板参数应该是类模板或别名模板的名称,表示为id表达式。
变量模板不符合这一要求。
您可以稍微欺骗一下并使用代理类型来选择模板:
template < typename Tuple, class Proxy>
struct PutTupleInTV;
template < typename... Types, class Proxy>
struct PutTupleInTV<std::tuple<Types...>, Proxy>
{
static constexpr auto value = Proxy::template value<Types...>;
};然后为了
template<typename...> struct foo{};
template<typename... Ts> constexpr foo<Ts...> foo_v{};
struct use_foo
{
template<typename... Ts>
static constexpr auto value = foo_v<Ts...>;
};你可以说
PutTupleInTV<tup, use_foo>::valuelive demo
发布于 2016-10-23 08:47:25
PutTupleInTV不是PutTupleInV的同名。您不是专门化模板PutTupleInV,而是使用专门化语法创建新的东西,称为PutTupleInTV。
https://stackoverflow.com/questions/40201020
复制相似问题