[英]C++ template function explicit specialization with multiple template types
I'd like to write a templatized function with two template types: one a bool and one a typename, and I'd like to specialize on the typename.我想编写一个具有两种模板类型的模板化 function:一种是 bool,一种是类型名,我想专门研究类型名。
eg, this is what I want, but specialized on just a couple types for T:例如,这就是我想要的,但只专注于 T 的几种类型:
template<bool b, typename T>
void foo(T val)
{
// do different stuff based on b and type of T.
}
without the bool in there, I can do something like this:没有那里的布尔,我可以做这样的事情:
template<typename T>
void bar(T val) {
static_assert(false, "not implemented");
}
template<>
void bar<short>(short val) {
printf("it's a short!\n");
}
I can't figure out the syntax for this, and the microsoft docs on specialization only cover the single-type case.我无法弄清楚它的语法,并且关于专业化的微软文档仅涵盖单一类型的情况。
template<bool B, typename T>
void foo(T const&)
{
static_assert(false, "not implemented");
}
template<bool B>
void foo(short)
{
printf("it's a short!\n");
}
However, this is not really specialisation, but overloading, which is completely appropriate.但是,这并不是真正的专业化,而是重载,这是完全合适的。 In fact, you could omit the general case.
事实上,你可以省略一般情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.