Quantcast
Viewing all articles
Browse latest Browse all 22118

ambiguous template instantiation error with gcc

For the code below I get an ambiguous template instantiation error with gcc.However, using Clang or Visual Studio the Code compiles fine. A full working example of the code can be found here:http://coliru.stacked-crooked.com/a/60ef9d73ce95e6f9

I have a class template that is build from an aggegate type

template<template<typename...> typename AggregateType, typename ...>struct MyClass;

The aggregate type is composed from a list of base classes, for example

template<typename ... Bases>struct Aggregate : Bases...{ };

I have defined two specialzations of MyClass. The first specialization is the common case and reads

// specialization for two argument list for the// aggregate typetemplate<template<typename...> typename AggregateType,      typename Base,      typename ... Bases1,      typename ... Bases2>struct MyClass<      AggregateType,      AggregateType<Bases1...>,      AggregateType<Base, Bases2...>>{      void func()      {       std::cout << "not specialized\n";      }};

A second specialization handels the case when the the second base list has only 1 argument

// specialization for the second argument list with length 1template<template<typename...> typename AggregateType,     typename Base,     typename ... Bases1>struct MyClass<    AggregateType,    AggregateType<Bases1...>,    AggregateType<Base>>{  void func()  {    std::cout << "specialized\n";  }};

Using MyClass with a second argument list of length 1, I expected the compiler to choose the second specialization of MyClass, since it is the more specilized template

class Foo {};class Bar {};int main(){  // this should give the not specialized class  using NotSpecialized = MyClass<Aggregate, Aggregate<Foo, Bar>, Aggregate<Foo, Bar>>;  NotSpecialized ns;  ns.func();  // this should give the specialized class  using Specialized = MyClass<Aggregate, Aggregate<Foo, Bar>, Aggregate<Foo>>;  Specialized s;  s.func();}

While the code works fine with Clang, gcc gives an ambiguous template instantiation error. How can I circumvent this error and still use gcc?If I remove the AggregateType template argument, the code also works with gcc, see http://coliru.stacked-crooked.com/a/c1f6edd5fab7df4d


Viewing all articles
Browse latest Browse all 22118


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>