Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 专门化引用类型的函数模板_C++_Templates_Specialization_Template Specialization_Reference Type - Fatal编程技术网

C++ 专门化引用类型的函数模板

C++ 专门化引用类型的函数模板,c++,templates,specialization,template-specialization,reference-type,C++,Templates,Specialization,Template Specialization,Reference Type,为什么这个输出是: #包括 模板void f(T参数) { 引用只是一个别名,而不是一个类型,它将第一个版本与T=int匹配,T=int是一个更好的选项。如果将T更改为T&,则int和int&参数都将调用第二个版本。表达式y和表达式z的类型都是int。表达式中出现的引用不会保留引用类型。相反,表达式的类型将是引用的类型,表达式为左值 因此,在这两种情况下,T被推导为int,因此根本不使用显式专门化 需要注意的是(除了像另一个人所说的那样,您应该真正使用重载之外),您的模板中有一个非引用函数参数

为什么这个输出是:

#包括
模板void f(T参数)
{ 

引用只是一个别名,而不是一个类型,它将第一个版本与T=int匹配,T=int是一个更好的选项。如果将T更改为T&,则int和int&参数都将调用第二个版本。

表达式
y
和表达式
z
的类型都是
int
。表达式中出现的引用不会保留引用类型。相反,表达式的类型将是引用的类型,表达式为左值

因此,在这两种情况下,
T
被推导为
int
,因此根本不使用显式专门化


需要注意的是(除了像另一个人所说的那样,您应该真正使用重载之外),您的模板中有一个非引用函数参数。在对参数类型进行任何
T
推断之前,参数类型将从数组转换为指向其第一个元素的指针(对于函数,参数将转换为函数指针)。因此,带有非引用函数参数的函数模板无论如何都不允许进行精确的推导。

我知道这不是答案,但是,我想你可以尝试一下,在结构中使用类似特征的方法:

template<typename T>
struct value_traits
{
    static void print(){std::cout << "General" << std::endl ;} 
};

template<>
struct value_traits<const long>
{
    static void print(){std::cout << "const long" << std::endl ;} 
};

template<>
struct value_traits<std::vector<unsigned char> >
{
    static void print(){std::cout << "std::vector<unsigned char>" << std::endl ; }
};

template<>
struct value_traits<const int>
{
       static void print(){std::cout << "const int" << std::endl ;} 
};
模板
结构值特征
{

静态void print(){std::cout可能是因为const int&比int&?@septgram:const int&?我不知道它是否有帮助,但是如果您更改模板以接受
t&
,那么
f(y)
f(z)
调用
int&
版本。@Daniel Gallagher:那么
f(2)就出现了编译器错误
f(y)
的输出是
int&
。最好的方法是使用重载
void f(int¶m)
如果我将T改为T&,当
f(2)
f(“text”)
+1时,我会出现编译器错误(感谢您在我回答时发现了错误)关于你的第二句话:这条规则是从哪里来的?我相信你,但是你有任何来源来解释这一点吗?谢谢。
template<typename T>
struct value_traits
{
    static void print(){std::cout << "General" << std::endl ;} 
};

template<>
struct value_traits<const long>
{
    static void print(){std::cout << "const long" << std::endl ;} 
};

template<>
struct value_traits<std::vector<unsigned char> >
{
    static void print(){std::cout << "std::vector<unsigned char>" << std::endl ; }
};

template<>
struct value_traits<const int>
{
       static void print(){std::cout << "const int" << std::endl ;} 
};