如何使用模板临时参数专门化模板 [英] How to specialize a template with template-tempate parameters
问题描述
最后编辑
我有一个带有模板的函数:
I have a function which takes a template:
template <template <typename ...> class P, typename ... Args>
void f(const P<Args...> &p)
{
std::cout << "Template with " << sizeof...(Args) << " parameters!\n";
}
它与到目前为止我测试过的任何种类的模板都可以很好地工作:
It works pretty good with any kind of templates I've tested so far:
f(std::valarray<int>{}); // Prints: "Template with 1 parameters!"
f(std::pair<char, char>{}); // Prints: "Template with 2 parameters!"
f(std::set<float>{}); // Prints: "Template with 3 parameters!"
f(std::map<int, int>{}); // Prints: "Template with 4 parameters!"
但是,让我们假设我想对模板进行专门化处理,因为它需要带有两个参数的模板,下面的代码不起作用:
But, let's suppose I want to specialize the template for when it takes a template with two parameters, the code below doesn't work:
template <>
void f<template <typename, typename> class P, typename A, typename B>(const P<A, B> &p)
{
std::cout << "Special case!\n";
}
parse error in template argument list
void f<template <typename, typename> class P, typename A, typename B>(const P<Args...> &p) { std::cout << "Template with " << sizeof...(Args) << " parameters!\n"; }
^
'P' does not name a type
void f<template <typename, typename> class P, typename A, typename B>(const P<Args...> &p) { std::cout << "Template with " << sizeof...(Args) << " parameters!\n"; }
^
expected ',' or '...' before '<' token
void f<template <typename, typename> class P, typename A, typename B>(const P<Args...> &p) { std::cout << "Template with " << sizeof...(Args) << " parameters!\n"; }
^
template-id 'f<<expression error> >' for 'void f(int)' does not match any template declaration
void f<template <typename, typename> class P, typename A, typename B>(const P<Args...> &p) { std::cout << "Template with " << sizeof...(Args) << " parameters!\n"; }
AFAIK使用其他类型的模板参数非常简单:
AFAIK it's pretty simple with the other types of template parameters:
// Type parameter
template <typename TYPE>
void f(TYPE) { std::cout << "Type!\n"; }
// Non-type parameter
template <int VALUE>
void f() { std::cout << "Value " << VALUE << "!\n"; }
// Type parameter specialization
template <>
void f(float) { std::cout << "Special type case!\n"; }
// Non-type parameter specialization
template <>
void f<666>() { static_assert(false, "Forbidden number!"); }
如何使用模板模板模板实现此功能?
How can I achieve this functionality with template-template templates?
As pointed by orlp and angew function templates cannot be partially specialised, so I should stick to object templates, here is my attempt:
template <template <typename ...> class P, typename ... Args>
struct c
{
using type = P<Args...>;
const std::size_t count = sizeof...(Args);
void f(const type &t)
{
std::cout << "Template with " << sizeof...(Args) << " parameters!\n";
}
};
template <template <typename, typename> class P, typename A, typename B>
struct c<P, A, B>
{
using type = P<A, B>;
void f(const type &t)
{
std::cout << "Spezialized 2 parameters!\n";
}
};
有效:
c<std::valarray, int> c_valarray_int;
c<std::pair, int, char> c_pair_int_char;
c<std::vector, int, std::allocator<int>> c_vector_int;
c<std::map, int, int> c_map_int_int;
c_valarray_int.f({}); // Prints: "Template with 1 parameters!"
c_pair_int_char.f({}); // Prints: "Spezialized with 2 parameters!"
c_vector_int.f({}); // Prints: "Spezialized with 2 parameters!"
c_map_int_int.f({}); // Prints: "Template with 2 parameters!" (expected)
但是,现在,我应该指定所有参数,而不是让编译器猜测全部内容,这不是悲剧.
But now, I should specify all the parameters instead of let the compiler guess the whole thing, well... that's not a tragedy.
推荐答案
您正在尝试对函数模板进行 partial 专业化—针对具有两个模板参数的 any 模板进行专业化.这在C ++中是不允许的.功能模板不能部分专门化.
You're attempting partial specialisation of your function template—specialisation for any template with two template parameters. That is not allowed in C++. Function templates cannot be partially specialised.
换一种方式来看:您为临时类型参数和模板非类型参数显示的显式专业化使用具体参数.要显式专门化您的功能模板,您可以执行以下操作:
Look at it another way: the explicit specialisations you've shown for tempalte type parameters and template non-type parameters use concrete arguments. To explicitly specialise your function template, you'd do something like this:
template <>
void f<std::pair, int, double>(const std::pair<int, double> &p)
{
std::cout << "Special case!\n";
}
在您的原始代码中,P
,A
和B
仍然是未绑定"的-它们是参数,而不是参数.显式专业化不能具有模板参数;必须将要专用的主模板的所有模板参数绑定到该专用提供的具体参数上.
In your original code, P
, A
and B
are still "unbound"—they're parameters, not arguments. An explicit specialisation cannot have template parameters; all template parameters of the primary template being specialised must be bound to concrete arguments provided by that specialisation.
要解决您的编辑问题,您仍然可以保留带有参数推论的函数模板接口,并将调用简单地转发到(可能是部分专用的)类模板,如下所示:
To address your edit: you can still keep the function template interface with argument deduction, and simply forward the call to a (potentially partially specialised) class template, like this:
template <template <typename ...> class P, typename ... Args>
struct c
{
using type = P<Args...>;
static const std::size_t count = sizeof...(Args);
static void f(const type &t)
{
std::cout << "Template with " << sizeof...(Args) << " parameters!\n";
}
};
template <template <typename, typename> class P, typename A, typename B>
struct c<P, A, B>
{
using type = P<A, B>;
static void f(const type &t)
{
std::cout << "Spezialized 2 parameters!\n";
}
};
template <template <class ...> class P, class... Args>
void f(const P<Args...> &t)
{
c<P, Args...>::f(t);
}
这篇关于如何使用模板临时参数专门化模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!