Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ and clang++ different behaviour when `std::make_index_sequence` and `std::index_sequence` are used for a template parameter default type

Another "who's right between g++ and clang++?" question for C++ standard gurus.

Given the following code

#include <utility>

template <std::size_t N, typename = std::make_index_sequence<N>>
struct foo;

template <std::size_t N, std::size_t ... Is>
struct foo<N, std::index_sequence<Is...>>
 { };

template <std::size_t N>
void bar (foo<N> const &)
 { }

int main()
 {
   bar(foo<42u>{});
 }

I see that g++ compile where clang++ gives the following error

tmp_003-14,gcc,clang.cpp:32:4: error: no matching function for call to 'bar'
   bar(foo<42u>{});
   ^~~
tmp_003-14,gcc,clang.cpp:27:6: note: candidate template ignored: could not match
      '__make_integer_seq' against 'integer_sequence'
void bar (foo<N> const &)
     ^
1 error generated.

As usual, the question is: who's right? g++ or clang++?

-- EDIT -- As pointed by HolyBlackCat (thanks!), some older version of clang++ compile this code where the newer don't.

I've tried with Wandbox and I see that clang++ compile from 3.4 (the first version supporting std::make_index_sequence/std::index_sequence) to 3.8.1. Starting from 3.9.1 gives the preceding error.

-- EDIT 2 -- Observe that the clang++ compilation error seems strictly bounded to the use of the first template argument in definition of the default value for the second.

In fact, changing

template <std::size_t N, typename = std::make_index_sequence<N>>
struct foo;

in

// ........................... now doesn't depends from N -->VVV
template <std::size_t N, typename = std::make_index_sequence<10u>>
struct foo;

both compilers compile.

like image 807
max66 Avatar asked Jul 21 '19 12:07

max66


1 Answers

This is plainly some sort of Clang/libc++ bug: the type std::make_index_sequence<…> isn’t __make_integer_seq, it’s… std::index_sequence<…>. Type aliases (and alias templates) are transparent, and deduction has always worked for std::vector despite its default (allocator) template argument.

like image 194
Davis Herring Avatar answered Oct 11 '22 23:10

Davis Herring