9

I don't understand why T cannot be deduced in this scenario:

template<class T>
class MyType
{
    T * data;
};

class MyOtherType
{
};

template<typename T>
struct MyType_OutArg
{
    typedef MyType<T> & type;
};

template<typename T>
void
DoSomething(typename MyType_OutArg<T>::type obj)
{

}

void func(MyType_OutArg<MyOtherType>::type obj)
{
    DoSomething(obj);
}

From GCC 4.7.1 with -std=c++14

<source>: In function 'void func(MyType_OutArg<MyOtherType>::type)':
26 : <source>:26:20: error: no matching function for call to 'DoSomething(MyType<MyOtherType>&)'
     DoSomething(obj);
                    ^
26 : <source>:26:20: note: candidate is:
19 : <source>:19:1: note: template<class T> void DoSomething(typename MyType_OutArg<T>::type)
 DoSomething(typename MyType_OutArg<T>::type obj)
 ^
19 : <source>:19:1: note:   template argument deduction/substitution failed:
26 : <source>:26:20: note:   couldn't deduce template parameter 'T'
     DoSomething(obj);
                    ^
Compiler returned: 1

Of course the following works:

DoSomething<MyOtherType>(obj);

but i'm unsure why it's necessary. Shouldn't the compiler have enough information?

user109078
  • 906
  • 7
  • 19
  • 1
    Well the compiler can't know whether there will be a specialization like `template<> struct MyType_OutArg { typedef MyType & type; };`, which could make deduction ambiguous. – cpplearner Jan 22 '18 at 18:35
  • 1
    @cpplearner Why not? The compiler knows if such a specialization exists before the definition of `DoSomething` because it encountered it, otherwise it doesn't call it anyways. I think the reason is more that the template deduction rules have limited complexity and "figure out what template instantiation has the proper member typedef to make this fit" is a bit too complex for those rules. – nwp Jan 22 '18 at 18:40

1 Answers1

8

This is because your case is a Non-deduced contexts.

Cited from http://en.cppreference.com/w/cpp/language/template_argument_deduction:

Non-deduced contexts

In the following cases, the types, templates, and non-type values that are used to compose P do not participate in template argument deduction, but instead use the template arguments that were either deduced elsewhere or explicitly specified. If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.

1) The nested-name-specifier (everything to the left of the scope resolution operator ::) of a type that was specified using a qualified-id

In your case, typename MyType_OutArg<T>::type will not participate in type deduction, and T is not known from elsewhere, thus this template function is ignored.

llllllllll
  • 16,169
  • 4
  • 31
  • 54