对隐式模板实例化感到困惑

5 c++ templates

这是C++ 03标准§14.7.1p5中的陈述:

如果重载解析过程可以在不实例化类模板定义的情况下确定要调用的正确函数,则未指定该实例化是否实际发生.[ 例如:

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
};
Run Code Online (Sandbox Code Playgroud)

- 结束例子 ]

我无法理解这一点.它有未定义的行为吗?

我发现了另一个类似的问题,我也不明白.在那里解释说正确的行为是不确定的,但这是什么意思?

这里: MSVC:隐式模板实例化,虽然未使用模板化构造函数

Pra*_*rav 0

在重载解析期间,确定在编写时调用的正确函数f(sr)没有void f(S<int>&);显式实例化类 template 的定义S,未指定您的类是否实际实例化。

未定义的行为和未指定的行为是两个完全不同的事情。

允许但不要求 S<int> 的实例化

例如:

template <class T =int> 
struct S 
{
  operator int();
};
Run Code Online (Sandbox Code Playgroud)

是允许的,但不是必需的。