5

This is the statement from the C++03 standard, ยง14.7.1p5:

If the overload resolution process can determine the correct function to call without instantiating a class template definition, it is unspecified whether that instantiation actually takes place. [Example:

template <class T> struct S {
       operator int();
};

void f(int);
void f(S<int>&);
void f(S<float>);

void g(S<int>& sr) {
        f(sr);     // instantiation of S<int> allowed but not required
                   // instantiation of S<float> allowed but not required
};

โ€”end example]

I am unable to understand this point. Does it have undefined behavior?

I found another similar problem, which I also don't understand. There it is explained that the correct behavior is undefined, but what does that mean?

here: MSVC: Implicit Template Instantiation, though templated constructor not used

Community
  • 1
  • 1

2 Answers2

2

Unspecified means that

  1. it's up to the compiler whether it will actually instantiate the templated class or not and
  2. the compiler designers must have chosen some strategy for dealing with such situations and
  3. the compiler designers are not required to document their choise.

Anyway this is correct behavior unlike undefined behavior which is erroneous behavior. See this related question for detailed explanation.

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Though the [C++ definitions](http://stackoverflow.com/q/2047172/54262) might help him more than C's, since he apparently has a copy of the C++ standard. โ€“  Oct 07 '10 at 10:44
  • I know Undefined/Unspecified/Implementation-defined behaviour warnings ..But i need a clarification about this program.. โ€“  Oct 07 '10 at 11:01
0

During overload resolution it is determined that the correct function to call when you write f(sr) is void f(S<int>&); without explicitly instantiating the definition of class template S, it is unspecified whether your class is actually instantiated.

Undefined behaviour and Unspecified behaviour are two completely different things.

instantiation of S< int > allowed but not required

For example:

template <class T =int> 
struct S 
{
  operator int();
};

is allowed but not required.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345