简体   繁体   English

C++ 模板专业化/部分和全部

[英]C++ Template Specialization / Partial and Full

I was trying to understand Template Partial and Full Specialization and have this below code:我试图了解模板部分和完全专业化并使用以下代码:

#include <iostream>
using namespace std;

//Template in C++is a feature. We write code once and use it for any data type including user defined data types.
//What if we want a different code for a particular data type - Template Specialization
template <class T, class Y>
void fun(T a, Y b)
{
    cout << "The main template fun(): " << a << " " <<  b << endl;
}
//If a specialized version is present, compiler first checks with the specialized version and then the main template.
//Compiler first checks with the most specialized version by matching the passed parameter with the data type(s) specified in a specialized version.
template<>
void fun(const char* a, const char* b)
{
    cout << "Specialized Template for int type: " << a << " " <<  b << endl;
}
//Partial specialization allows to specialize only some arguments of a class template, as opposed to explicit full
template<class T>
void fun(T a, int b)
{
    cout << "Partial Template specialization for int type: " << a << " " << b << endl;
}
int main()
{
    fun<int, int>('a', 10);
    fun< const char*, const char*>("Hello", "Morning");
    fun<float, float>(10.14, 19.89);
}

Note that in main I am specifying the data types and below is the output:请注意,在 main 中我指定了数据类型,下面是 output:

The main template fun(): 97 10
Specialized Template for int type: Hello Morning
The main template fun(): 10.14 19.89  

But when I execute the main code below way:但是当我以下面的方式执行主要代码时:

int main()
{
    fun('a', 10);
    fun("Hello", "Morning");
    fun(10.14, 19.89);
}

This is the output I get:这是我得到的 output:

Partial Template specialization for int type: a 10
Specialized Template for int type: Hello Morning
The main template fun(): 10.14 19.89 

So what does the actual C++ Template Partial / Full specialization states - do we need to specify the data types in template argument to invoke - also in many websites I have seen following signature for Partial specialization:那么实际的 C++ 模板部分/完全专业化状态是什么 - 我们是否需要在模板参数中指定要调用的数据类型 - 在许多网站上我也看到了部分专业化的以下签名:

template<class Y, const char*>
void fun(Y a, const char* b)

Rather than而不是

template<class Y>
void fun(Y a, const char* b)

Similarly for full specialization - what is the exact way to write and call Partial / Full template specialized function / class?同样对于完全专业化 - 编写和调用部分/完整模板专业化 function / class 的确切方法是什么?

The comments cover the general principles, but there are specific questions here worth answering.评论涵盖了一般原则,但这里有一些具体问题值得回答。 First, a lemma: the template parameters of a function template need not be related to its parameter types:首先,引理:function 模板的模板参数不需要与其参数类型相关

template<class T>
T make_1() {return T(1);}
auto v=make_1<std::vector<std::string>>(); // {""}
template<class T,class U>
void f(std::conditional_t<sizeof(T)<sizeof(U),int,long> i);

This is why your “partial specialization” is not a candidate for fun<int, int>(…) .这就是为什么您的“部分专业化”不是fun<int, int>(…)的候选者。 The template arguments aren't just instructions for how to build the parameter list one by one, but rather must match the actual template parameters for the function template in question.模板 arguments 不仅说明如何逐个构建参数列表,而且必须与所讨论的 function 模板的实际模板参数相匹配。 There's only one of those, so they don't match.只有其中一个,所以它们不匹配。 True partial specializations still use the primary template's template parameter list;真正的偏特化仍然使用主模板的模板参数列表; they just match against certain patterns in the list of arguments for it.它们只是匹配 arguments 列表中的某些模式

Meanwhile,同时,

template<class Y, const char*>
void fun(Y a, const char* b)

is valid but doesn't mean what you think for the same reason: const char* in the template parameter list introduces an unnamed (non-type) template parameter of type const char* , so that you'd have to use it like是有效的,但并不意味着您的想法出于同样的原因:模板参数列表中的const char*引入了类型为const char*未命名(非类型)模板参数,因此您必须像这样使用它

char global;
void g() {fun<char,&global>('a',"foo");}

(with no deduction for Y ,), only to have that &global ignored entirely, (没有扣除Y ,),只是完全忽略了&global

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM