Template specialization selection/ambiguity

Started by
1 comment, last by Juliean 6 years, 8 months ago

Hi, quick question.. Why is the type of "fooBar" ambiguous?


template< typename T >
struct Bar
{
};

template< typename T1, typename T2 >
struct Foo;

template< typename T >
struct Foo< T, Bar< T > >
{
};

template< typename T >
struct Foo< float, Bar< T > >
{
};

Foo< float, Bar< float > > fooBar;

I would expect that the second version of Foo is more specialized than the first...

(I am building a compiler and I would like to understand the template specialization selection algorithm)

Cheers!

Advertisement

For a quick reference, you can look at the summary of the specialization rules here:

http://en.cppreference.com/w/cpp/language/partial_specialization

Under "Partial Ordering", you'll find an explanation of how the process of specialization selection works. If I understand it correctly, with the proposed specialization selection algorithm, both of your templates could potentially be a subset of each other and thats why it fails - though I'm not 100% sure, but you can just try it out and see for yourself :)

This topic is closed to new replies.

Advertisement