-3

After I read the answer for this question, I am still asking question. The answer talks about location of point of instantiation as defined in [temp.point] but it is not evaluated if the instantiations are required.

[temp.inst] specifies when a function template specialization instantiation is required, the general rule is:

An implementation shall not implicitly instantiate a function template, a variable template, a member template, a non-virtual member function, a member class,[...], unless such instantiation is required.

Moreover in some paragraph of this section of the standard, instantiation of declaration and instantiation of definition are considered separately.

Let's consider this code:

static void Yeap0(int);

template <class T>
void Yeap(T a){
    Yop(a);
}

template <class T>
auto Yeap2(T a){
    Yop(a);
    return 0;
}
namespace x{
    struct y{};
}

int main() {
    Yeap0(0);//ok no definition required here
    x::y ax{};
    Yeap(ax); //the declaration is sufficient, 
    Yeap2(ax); //need to deduce auto => definition required
               //compilation error
    return 0;
}

namespace x{
    void Yop(y){}
}

static void Yeap0(int){}

Gcc, Clang and MSVC produce only an error for Yeap2(ax), complaining that Yop is not defined at the point of instantiation of Yeap2.

But this error is not generated for Yeap(ax). From basic consideration this seems logical, only the declaration is needed, as for the none template function Yeap0.

But the lecture of [temp.inst]/4 let me perplex. It could be understood that the instantiation of Yeap is also required. But it seems that compilers have taken a more clever path.

Is the compilers behavior an extension?


Note: I will not accept the "no diagnostic required" answer. This would be an insult to intelligence: Could one believe that 3 compilers have taken special care to shut down diagnostics for case like Yeap(ax) but not for Yeap2(ax)?

Oliv
  • 17,610
  • 1
  • 29
  • 72
  • Maybe it is not "shut down diagnostics". For `Yeap2`, the compiler has to look into the function, to determine return type. For `Yeap`, it doesn't have to. It is absolutely reasonable that all three compilers behave the same in this regard. – geza Sep 18 '18 at 20:38
  • 1
    Obviously compiler devs don't just shut down diagnostics for a special case. The standard uses NDR for things that are impossible/very hard to prove and sometimes in some cases diagnostics are easy for the compiler, and sometimes not. Even EDG has the same behavior. – Rakete1111 Sep 18 '18 at 20:42
  • @T.C. You should read the standard, compiler are doing the right thing: `Yeadp(ax)` cause overload resolution see [expr.call], which according to [temp.inst]/9 *If a function template or a member function template specialization is used in a way that involves overload resolution, a declaration of the specialization is implicitly instantiated*. The this instantition is performed at the point of instantiation [temp.point] specify where is the point of instantiation: after main, it does not say what is instantiated or if the instantiation is needed. – Oliv Sep 19 '18 at 06:21

1 Answers1

1

The compiler complaining really comes from the fact that the function returns auto.

In the auto case you really hit

unless such instantiation is required.

which must come from the requirement dcl.spec.auto.

To verify that assessment you can replace in your code Yeap2 by:

template <class T>
auto Yeap2(T a){
    Yeap(a);
    return 0;
}

and there is no compilation error.

PilouPili
  • 2,601
  • 2
  • 17
  • 31