gpt4 book ai didi

c++ - 真的允许函数模板特化吗?

转载 作者:行者123 更新时间:2023-12-05 09:35:53 35 4
gpt4 key购买 nike

根据 this article ,上面写着(强调我的):

Partial template specialization allows us to specialize classes (butnot individual functions!)

似乎函数部分模板特化是不允许的。这真的是正确的吗?

令我困惑的是为什么这些代码片段可以编译成功:

//demo1.cpp
//first code snippet(https://coliru.stacked-crooked.com/a/0868610b5be94f2c)
//Why this function template specialization compiles?
//Because this is full template specialization other than partial template specialization?Am I right?
#include <iostream>
#include <string.h>
using namespace std;

template <class T>
void f(T)
{
T d;
std::cout <<"first one" << std::endl;
}

template <>
void f<int>(int)
{
int d;
std::cout <<"second one" << std::endl;
}

int main()
{
f(5.0);
f(1);
f('a');
}

另一个例子:

//demo2.cpp(https://coliru.stacked-crooked.com/a/7b1a94ad377ac1f6)
//Why this function template specialization compiles?
//I think it's function partial template specialization indeed.
#include <vector>
#include <iostream>
using namespace std;

//function template
template<typename T, class N> void compare(T num1, N num2) {
cout << "standard function template" << endl;
if(num1>num2)
cout << "num1:" << num1 << " > num2:" << num2 <<endl;
else
cout << "num1:" << num1 << " <= num2:" << num2 << endl;
}

//function partial template specialization
template<class N> void compare(int num1, N num2) {
cout<< "partitial specialization" <<endl;
if (num1>num2)
cout << "num1:" << num1 << " > num2:" << num2 << endl;
else
cout << "num1:" << num1 << " <= num2:" << num2 << endl;
}

int main() {
compare<int,int>(30,31);//call compare<int,int>(int num1, int num2)

compare(5,9); //call compare<int>(int num1, int num2)

compare(30,'1'); //call compare<char>(int num1, char num2)

}

为什么 this code snippet无法编译,而上述两个代码片段编译成功?

#include <vector>
#include <iostream>
using namespace std;

template <class T1, class T2>
void f(){}

template <class T2>
void f<int, T2>(){} //It's function template sepcialization, isn't it? Why it could not be compiled?

int main() {}

错误信息如下:

main.cpp:9:6: error: non-type partial specialization 'f<int, T2>' is not allowed
9 | void f<int, T2>(){}
| ^~~~~~~~~~

已编辑:感谢 eerorika 的详细解释。

又提出一个问题,如何区分重载模板和部分特化函数模板?

换句话说,为什么 template<class N> void compare(int num1, N num2)是模板重载 template <class T2> void f<int, T2>(){}正在尝试部分特化函数模板?

最佳答案

Is function template specialization really allowed?

是的,但不是部分特化。

It seems that function partcial template specialization is not allowed.Is it really correct?

如果您的意思是部分特化,那确实不允许用于函数模板。

// Why this fucntion template specialization compiles? 
// Because this is full template specialization ...?Am I right?

是的。

//Why this fucntion template specialization compiles? 
//I think it's function partcial template specialization

不,那根本不是模板特化。那是一个单独的模板。这是一个过载。

Why this code snippet could not been compiled

因为它试图部分特化一个函数模板。


how to distinguish the overloading templates and partially specialising function templates?

函数特化的语法是:

template<>
ReturnType template_name<template_argument_list>(parameter_list) {
// ^^^^^^^^^^^^^^^^^^^^^^^^

如果语法不是这样,那么它就不是函数特化。请注意您的示例中缺少的突出显示部分不是特化。如果该部分被省略,这将成为主要模板定义。

关于c++ - 真的允许函数模板特化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65535818/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com