C ++ template function argument output and function resolution

Today I just want to raise the question about the argument of the C ++ template function template and the resolution of template overload in C ++ 11 (I use vs2010 sp1). I defined two template functions as shown below:

function # 1:

template <class T>
void func(const T& arg)
{
    cout << "void func(const T&)" <<endl;
}

function # 2:

template <class T>
void func(T&& arg)
{
   cout << "void func(T&&)" <<endl;
}

Now consider the following code:

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 underlying the definition of the overload function in these scenarios.

I disagree with the two answers below. I think the const int example is different from the string example. I can slightly change #function 1 to see that the type being deduced is on the ground

 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 int, const "const T &" "" const int; , "const T &" . int & const ( int * const)

+4
2

const. F1, F2 , F2 , . , const lvalue, F2. , lvalue const, F1 . const int .

+5

, # 2 T & T && rvalue lvalue. .

//Function #2 is selected... why?
//Why not function #1 or ambiguous...
{func(0);}

0 int&& - T&&

//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")}

"feng" const char(&)[5] - const T& 1- . (&) , .

//Here function#2 is selected in which T is deduced as char(&)[5]
{char array[] = "feng"; func(array);}

array - char(&)[5] - T&

+1

All Articles