7

I created 4 classes:

class C1 {};
class C2 {};
class C3 {};
class C4 {};

And template functions based on these classes:

template<class T1, class T2>
T2 F() {
    std::cout << "F<T1, T2>" << std::endl;
    return T2();
}

template<class T>
T F();

template<>
C1 F<C1>() {
    std::cout << "F<C1>" << std::endl;
    return F<C3, C1>();
}

template<>
C2 F<C2>() {
    std::cout << "F<C2>" << std::endl;
    return F<C4, C2>();
}

This gives me compile error:

explicit specialization 'C1 F<C1>(void)' is not a specialization of a function template
explicit specialization 'C2 F<C2>(void)' is not a specialization of a function template

However, if I change the name of the 1st function F to a different name F1:

template<class T1, class T2>
T2 F1() {
    std::cout << "F<T1, T2>" << std::endl;
    return T2();
}

template<class T>
T F();

template<>
C1 F<C1>() {
    std::cout << "F<C1>" << std::endl;
    return F1<C3, C1>();
}

template<>
C2 F<C2>() {
    std::cout << "F<C2>" << std::endl;
    return F1<C4, C2>();
}

or change the return type of these functions to be the same (like int):

template<class T1, class T2>
int F() {
    std::cout << "F<T1, T2>" << std::endl;
    return 5;
}

template<class T>
int F();

template<>
int F<C1>() {
    std::cout << "F<C1>" << std::endl;
    return F<C3, C1>();
}

template<>
int F<C2>() {
    std::cout << "F<C2>" << std::endl;
    return F<C4, C2>();
}

Then the compile succeeds, and I can successfully call:

F<C1>();
F<C2>();

And get expected output:

F<C1>
F<T1, T2>
F<C2>
F<T1, T2>

The question is why the 1st code doesn't compile but the 2nd and the 3rd code compiles.

In https://en.cppreference.com/w/cpp/language/function_template I found:

Two function templates with the same return type and the same parameter list are distinct and can be distinguished with explicit template argument list.

I guess that's why the 3rd code compiles as the return types are the same. But it doesn't explain why the 1st code doesn't compile just because the return types are different.

And even though it's because the return types can't be different, it doesn't explain why the 2nd code compiles as F<C1> and F<C2> has different return types there.

2
  • Which compiler are you using? When I try to compile your 1st code as-is, I do not get the "... is not a specialization of ..." errors that you claim, but instead I get "undefined reference to ..." errors on actual calls to F<C1>() and F<C2>(). Mar 22 at 20:41
  • 1
    @RemyLebeau gcc 12.2 works fine. clang and msvc however...
    – AndyG
    Mar 22 at 20:43

1 Answer 1

4

When you write the declaration

template<>
C1 F<C1>() { /*...*/ };

the compiler has to figure out which function template named F you are trying to specialize. This is done via template argument deduction similar to how it works in function calls. In particular there is no assumption that the template argument list <C1> is the complete template argument list of the specialization. It may be partial and may also be omitted just like in function calls.

The candidates are template 1:

template<class T1, class T2>
T2 F() {
    std::cout << "F<T1, T2>" << std::endl;
    return T2();
}

and template 2:

template<class T>
T F();

While deduction in a function call considers pairs of call arguments and function parameters, for explicit specialization matching the type of the function template itself is compared with the type of the declared specialization. (See [temp.deduct.decl].)

The type of the specialization here is C1(). The type of template 1 is T2() and the type of template 2 is T(). You specified T = C1 explicitly in the specialization, so there is nothing left to be deduced and the types match. You also specified T1 = C1 explicitly and deducing T2() against C1() gives T2 = C1.

Therefore both templates match: For template 1 it matches the specialization F<C1, C1> and for template 2 it matches the specialization F<C1>.

So for now it is ambiguous which specialization you are declaring.

Then partial ordering of function templates is considered to decide whether template 1 or template 2 is more specialized and should therefore be preferred. For this, again, the full types of the (transformed) templates are considered, see [temp.deduct.partial]/3.3. In partial ordering the explicitly specified template arguments are not relevant, but T() can be deduced against T2() in both directions. The only issue is that T1 would not be deduced to anything, but that is specifically ignored in partial ordering if the template parameter does not appear in the type (see [temp.deduct.partial]/12). So partial ordering will also consider both equally specialized.

As a result, it is ambiguous which function template you want to specialize.

You don't have the same issue when calling the function templates, because the return types don't participate in deduction in a function call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.