Class nested inside a template class as template function argument type

C

claudiu

Hi,

I run into a bit of trouble trying to compile the code below.

template<typename T>
struct Out
{
struct In{};

};

template<typename T>
void use(const typename Out<T>::In& r){}

void test()
{
use(Out<int>::In());

}

Using Comeau compiler I get the error:

line 13: error: no instance of function template "use" matches the
argument list
The argument types that you used are: (Out<int>::In)
use(Out<int>::In());
^

It looks like the template function use cannot deduce the argument
type but I can't understand why. Maybe someone out there can shed some
light on this?

Many thanks,
Claudiu
 
M

Michael Doubez

Hi,

I run into a bit of trouble trying to compile the code below.

template<typename T>
struct Out
{
  struct In{};

};

template<typename T>
void use(const typename Out<T>::In& r){}

void test()
{
  use(Out<int>::In());

}

Using Comeau compiler I get the error:

line 13: error: no instance of function template "use" matches the
          argument list
            The argument types that you used are: (Out<int>::In)
    use(Out<int>::In());
    ^

It looks like the template function use cannot deduce the argument
type but I can't understand why. Maybe someone out there can shed some
light on this?

IIRC it is a non deduceable case.

The standard lists the ways a function template parameter may be
deduced and a dependant name is not part of it.

But it may be deduced from other parameters.
 
M

Marc

claudiu said:
I run into a bit of trouble trying to compile the code below.

template<typename T>
struct Out
{
struct In{};

};

People often hit this with std::vector said:
template<typename T>
void use(const typename Out<T>::In& r){}

void test()
{
use(Out<int>::In());

}

Using Comeau compiler I get the error:

line 13: error: no instance of function template "use" matches the
argument list
The argument types that you used are: (Out<int>::In)
use(Out<int>::In());
^

You can call use<int>(...), or change the code to:

template<typename T>
struct Out_In { };
template<typename T>
struct Out
{
typedef Out_In<T> In;
};
template<typename T>
It looks like the template function use cannot deduce the argument
type but I can't understand why. Maybe someone out there can shed some
light on this?

Imagine that I add a specialization:
template<> struct Out<double>
{
typedef Out<int>::In In;
};

Should the compiler deduce T as int or double now? The compiler would
need to try every possible value of T to see if one has a type In that
matches, too many bad things can happen, deduction was thus defined to
only apply to direct types where it can be easily checked.
 
C

claudiu

Thanks Michael, thanks Marc,

I now found the standard paragraph describing this and the example
makes it clear why this should be the case.

Regards,
Claudiu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top