Choosing between a template instance with pointer arguments

Take a look at the following test code:

template<class T> struct Wrap {};

template<typename T> inline
void fun (T *&Int)          // **choice 1**
{}

template<typename T> inline
void fun (Wrap<T> *&Int)    // **choice 2**
{}

int main()
{
  int i = 6;
  fun((char*&)(i));         // **call 1**
  fun((Wrap<char>*&)(i));   // **call 2**
}

When I run this code in linux g ++, it works as expected. When fun () is called with char * &, it calls select function 1 straight forward. However, I am interested in when we call fun () with Wrap <char> * & and it calls choice 2. Although choices 1 and 2 are both valid for the second call, the compiler manages to select a slightly better rival → choice 2 (because it exist).

Question : is it guaranteed that the same behavior will be preserved for any other compiler for C ++? If not, is there another alternative to make it deterministic?

+5
4

, . . , .

fun( (Wrap<char>*&) i ) , , :

template <typename T> void fun( T*& );      // with T == Wrap<char>
template <typename T> void fun( Wrap<T>*& ) // with T == char

, . , : , , . @LiKao .

+2

, , , .. T*& T*, Wrap<T>*& Wrap<T>*. , , , , , , , .

+6

-, , , , Wrap<T> , T, 2 " 2", .

+3

One thing that can make this even more problematic is that the rules for specialized template classes and template functions are very different. This is done to overload the functions of the template, while there is no way to overload the classes. Since I'm not so sure about this topic, I will just contact someone who can explain this in more detail:

Herb Sutter: "Why Not Specialize Function Templates?"

+1
source

All Articles