Template function returning dependent type fails to compile  
Author Message
RuneM





PostPosted: Visual C++ Language, Template function returning dependent type fails to compile Top

When I try to compile the code below I get the following error (VS 2005 SP1):

error C2893: Failed to specialize function template 'T::value_type::{ctor} f(T)'

1> With the following template arguments:

1> 'N'

code:

struct X {};

template <typename T>

struct C {

typedef T value_type;

};

typedef C<C<X> > N;

template<typename T>

typename T::value_type::value_type f(T t) {

return typename T::value_type::value_type();

}

int main()

{

N n;

f(n);

return 0;

}



Visual C++11  
 
 
Mike Danes





PostPosted: Visual C++ Language, Template function returning dependent type fails to compile Top

It seems to be a bug. You can report it here

http://connect.microsoft.com/VisualStudio

From the error message I get that VC++ thinks that value_type::value_type from the return type of function f is a constructor even though you use typename. A quick (and really ugly solution) is to add another typedef to C template:

typedef T value_type2;

and define f as

template <typename T>

typename T::value_type::value_type2 f(T t)

{

return T::value_type::value_type(); //value_type2 not needed here.
}

Maybe a better solution is this:

template<typename T>

struct f_return

{

typedef typename T::value_type::value_type f_return_type;

};

template<typename T>

typename f_return<T>::f_return_type f(T t)

{

return typename T::value_type::value_type();

}

It has the advantage that it keeps the C struct unchanged.