Jump to content
  • Advertisement
Sign in to follow this  
Shnoutz

C++ Template specialization selection/ambiguity

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!