Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

c++ - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...