Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
350 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...