当`std :: make_index_sequence`和`std :: index_sequence`用于模板参数默认类型时,g ++和clang ++的行为不同 [英] g++ and clang++ different behaviour when `std::make_index_sequence` and `std::index_sequence` are used for a template parameter default type
问题描述
另一个谁在g ++和clang ++之间? C ++标准专家的问题。
Another "who's right between g++ and clang++?" question for C++ standard gurus.
给出以下代码
#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>{});
}
我看到g ++在clang ++编译时出现以下错误
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.
和往常一样,问题是:谁是对的? g ++或clang ++?
As usual, the question is: who's right? g++ or clang++?
-编辑-正如HolyBlackCat所指出的(谢谢!),一些较旧版本的clang ++会在以下位置编译此代码:
-- EDIT -- As pointed by HolyBlackCat (thanks!), some older version of clang++ compile this code where the newer don't.
我尝试了Wandbox,发现clang ++从3.4编译(第一个支持 std :: make_index_sequence
/ std :: index_sequence
)转换为3.8.1。从3.9.1开始会出现上述错误。
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.
-编辑2-请注意,clang ++编译错误似乎严格限制了使用范围
-- 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.
实际上,更改
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.
推荐答案
这显然是某种Clang / libc ++错误:类型 std :: make_index_sequence<…>
不是 __ make_integer_seq
,它是… std :: index_sequence<…>
。类型别名(和别名模板)是透明的,并且推导对 std :: vector
一直有效,尽管其默认为(分配器)模板参数。
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.
这篇关于当`std :: make_index_sequence`和`std :: index_sequence`用于模板参数默认类型时,g ++和clang ++的行为不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!