14

I have this function template:

template <class TemplateArgument, template<class> class TemplateType>
TemplateArgument f(const TemplateType<TemplateArgument>& arg)
{
    return TemplateArgument();
}

When used like this, it fails to compile:

struct A {};
template <typename T> struct S {};
template <typename T> struct B : public S<T> {};

struct C : public B<A> {};

int main()
{
    f(C());
    return 0;
}

And the error message is:

<source>: In function 'int main()':

<source>:15:10: error: no matching function for call to 'f(C)'

     f(C());

          ^

<source>:2:18: note: candidate: template<class TemplateArgument, template<class> class TemplateType> TemplateArgument f(const TemplateType<TemplateArgument>&)

 TemplateArgument f(const TemplateType<TemplateArgument>& arg)

                  ^

<source>:2:18: note:   template argument deduction/substitution failed:

<source>:15:10: note:   'const TemplateType<TemplateArgument>' is an ambiguous base class of 'C'

     f(C());

          ^

Happens with GCC (any version) and clang (any version). Does not happen with MSVC. Live demo: https://godbolt.org/g/eWxeHJ

Why does this error occur? I fail to see any ambiguity, the "ambiguous base class" error usually occurs in multiple inheritance situations, does it not? How can I make my code compile (deduce template arguments correctly)?

Note that I cannot edit the A, B, C, S classes and their relation to each other, I can only edit my function f() to accept these classes properly.

9
  • 3
    do you still need TemplateType? struct C is not templated.
    – Joseph D.
    Commented Jun 24, 2018 at 11:01
  • 6
    The compiler is not sure whether to deduce args type as B<A> or S<A>.
    – r3mus n0x
    Commented Jun 24, 2018 at 11:11
  • 1
    @r3mus_n0x On the one hand I agree with you on the other hand shouldn't compiler choose the most derived of the alternatives?
    – navyblue
    Commented Jun 24, 2018 at 13:49
  • 3
    @navyblue I wouldn't expect it to do anything automatically that is not explicitly specified in the standard. I've never read or heard of such a rule for template deduction.
    – super
    Commented Jun 24, 2018 at 13:57
  • @r3musn0x: oh, I see now... I, too, expected it to deduce the most derived class. Do you think it's a mistake in the C++ standard that the compiler isn't required to deduce this case automatically? Or is such deduction incompatible with the existing standard for some reason? Commented Jun 24, 2018 at 15:25

1 Answer 1

9

The compiler is not sure whether to deduce args type as B<A> or S<A>. I'm not sure about this specific case but MSVC is known to violate the standard especially when it comes to templates.

As for your function, you need to resolve this ambiguity yourself by explicitly casting to the appropriate base:

f((const B<A> &)C());

or by specifying template parameters explicitly:

f<A, B>(C());

Generally whenever there is any ambiguity in the language it is never automatically resolved by the compiler because it would just be a speculation about what exactly did user intend, which might be right in some cases and completely wrong in others.

2
  • 3
    @VioletGiraffe, the second parameter is a template template parameter so it should be f<A, B>(C()), that is should be substituted with a non-instantiated template name.
    – r3mus n0x
    Commented Jun 24, 2018 at 15:50
  • Thanks, learned something new about template deduction today (and also, about the importance of paying attention to details!) Commented Jun 24, 2018 at 15:53

Your Answer

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

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