问题描述
今天我只想提出一个问题C ++模板函数参数演绎和模板函数重载解析在c ++ 11(我使用vs2010 sp1)。
我已经定义了两个模板函数如下:
Today I just want to raise a question on C++ template function argument deduce and template function overload resolution in c++ 11 (I am using vs2010 sp1). I have defined two template functions as below:
function#1:
function #1:
template <class T>
void func(const T& arg)
{
cout << "void func(const T&)" <<endl;
}
function#2:
function #2:
template <class T>
void func(T&& arg)
{
cout << "void func(T&&)" <<endl;
}
现在考虑下面的代码:
int main() {
//I understand these first two examples:
//function #2 is selected, with T deduced as int&
//If I comment out function #2, function#1 is selected with
//T deduced as int
{int a = 0; func(a);}
//function #1 is selected, with T is deduced as int.
//If I comment out function #1, function #2 is selected,
//with T deduced as const int&.
{const int a = 0; func(a);}
//I don't understand the following examples:
//Function #2 is selected... why?
//Why not function #1 or ambiguous...
{func(0);}
//But here function #1 is selected.
//I know the literal string "feng" is lvalue expression and
//T is deduced as "const char[5]". The const modifier is part
//of the T type not the const modifier in "const T&" declaration.
{func("feng")}
//Here function#2 is selected in which T is deduced as char(&)[5]
{char array[] = "feng"; func(array);}
}
我只想知道引导函数重载解析在这些情况下。
I just want to know the rules behind guiding the function overloading resolution under these scenarios.
我不同意下面的两个答案。我认为const int示例不同于字符串示例。我可以修改#function 1一点,看看地球上推导出的类型
I don't agree with the two answers below.I think the const int example is different from the literal string example. I can modify the #function 1 a bit to see what’s the deduced type on earth
template <class T>
void func(const T& arg)
{
T local;
local = 0;
cout << "void func(const T&)" <<endl;
}
//the compiler compiles the code happily
//and it justify that the T is deduced as int type
const int a = 0;
func(a);
template <class T>
void func(const T& arg)
{
T local;
Local[0] = ‘a’;
cout << "void func(const T&)" <<endl;
}
//The compiler complains that "error C2734: 'local' : const object must be
//initialized if not extern
//see reference to function template instantiation
//'void func<const char[5]>(T (&))' being compiled
// with
// [
// T=const char [5]
// ]
Func("feng");
,const修饰符在const T&声明中, const int;而在文字字符串示例中,我不知道const修饰符在const T&声明中的位置。声明一些类似int& amp; const(但有意义的是声明int * const)
in the const int example, the const modifier in the "const T&" declaration eats up "the constness" of const int; while in the literal string example, I don’t know where the const modifier in the "const T&" declaration goes. It is meaningless to declare some like int& const (but it is meaningful to declare int* const)
推荐答案
这里的技巧是 / code>。 F1和F2都可以接受任何类型的任何值,但F2是一个更好的匹配,因为它是完美的转发。因此,除非值是
const
lvalue,F2是最好的匹配。但是,当lvalue为 const
时,F1是更好的匹配。这就是为什么它是首选的const int和字符串文字。
The trick here is the const
. Both F1 and F2 can accept any value of any type, but F2 is a better match in general, because it's perfect forwarding. So unless the value is a const
lvalue, F2 is the best match. However, when the lvalue is const
, F1 is the better match. This is why it's preferred for the const int and the string literal.
这篇关于c ++模板函数参数推导和函数解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!