Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can template inheritance from its another specialization be a circular dependency or is it a clang's bug

Consider code below:

#include <iostream>
#include <utility>
#include <tuple>

template <class TargetIndices, class SourceIndices>
struct assign;

template <size_t... TargetIs, size_t... SourceIs>
struct assign<std::index_sequence<TargetIs...>, std::index_sequence<SourceIs...>>: assign<std::index_sequence<TargetIs>, std::index_sequence<SourceIs>>... {
   template <class TargetTuple, class SourceTuple>
   assign(TargetTuple &target, const SourceTuple &source): assign<std::index_sequence<TargetIs>, std::index_sequence<SourceIs>>(target, source)... { }
};

template <size_t FirstTI, size_t FirstSI>
struct assign<std::index_sequence<FirstTI>, std::index_sequence<FirstSI>> {
   template <class TargetTuple, class SourceTuple>
   assign(TargetTuple &target, const SourceTuple &source) {
      std::get<FirstTI>(target) = std::get<FirstSI>(source);
   }
};


int main() {
   std::tuple<int, int, int> t1 = std::make_tuple(0,0,0), t2 = std::make_tuple(1,2,3);
   assign<std::index_sequence<0,2>, std::index_sequence<2,0>>(t1, t2);
   std::cout << std::get<0>(t1) << " " << std::get<1>(t1) << " " << std::get<2>(t1) << std::endl;
}

clang++ states that there is a circular dependency at the inheritance. g++ compiles it without any problems. Which compiler is right?

like image 533
W.F. Avatar asked Jun 19 '16 15:06

W.F.


1 Answers

It looks like a clang bug.

A minimal reproduction looks like this:

template <int...> struct q {};

template <int... Is> using w = q<Is...>;

template <class T>
struct a;

template <int F>
struct a<w<F>> {};

template <int... T>
struct a<w<T...>>: a<w<T>>... {}; // g++ OK, clang++ breaks

The template alias is essential here. When the original template is used in the last line instead of the template alias, it compiles:

struct a<q<T...>>: a<q<T>>... {}; // g++, clang++ OK
like image 173
n. 1.8e9-where's-my-share m. Avatar answered Sep 18 '22 12:09

n. 1.8e9-where's-my-share m.