[英]g++ and clang++ different behaviour with variadic container
In order to practice with C++11, I'm playing with variadic templates.为了练习 C++11,我正在使用可变参数模板。
In particular, I'm playing with a sort of recursive variadic container class ( onion
) and a function that returns the number of template types ( func()
).特别是,我正在使用一种递归可变参数容器类 ( onion
) 和一个返回模板类型数量的func()
)。
I came across a case that the clang++ (3.5.0) can't compile while the g++ (4.9.2) compiles and runs with no problems.我遇到了一个案例,clang++ (3.5.0) 无法编译,而 g++ (4.9.2) 编译和运行没有问题。
I have simplified it as follows我已将其简化如下
#include <iostream>
template <typename F, typename ... O>
struct onion;
template <typename F, typename S, typename ... O>
struct onion<F, S, O...>
{
F first;
onion<S, O...> others;
};
template <typename L>
struct onion<L>
{ L last; };
template <typename ... Args>
std::size_t func (onion<Args...> const &)
{ return sizeof...(Args); }
int main()
{
auto o = onion<int, char const *, float> { 23, { "abcd", {34.0f}} };
std::cout << func(o) << '\n';
return 0;
}
clang++ (3.5.0) gives me the following compiler error clang++ (3.5.0) 给了我以下编译器错误
test.cpp:28:17: error: no matching function for call to 'func'
std::cout << func(o) << '\n';
^~~~
test.cpp:20:13: note: candidate template ignored: substitution
failure [with Args = <>]: too few template arguments for class template
'onion'
std::size_t func (onion<Args...> const &)
^ ~~~~~
1 error generated.
g++ (4.9.2) compiles without problem and, running, output 3
. g++ (4.9.2) 编译没有问题,运行,输出3
。
My question is: who's right?我的问题是:谁是对的?
clang++, giving an error, or g++, compiling? clang++,给出错误,或者 g++,编译?
I add that if I rewrite func()
in this way我补充说,如果我以这种方式重写func()
template <typename X, typename ... Args>
std::size_t func (onion<X, Args...> const &)
{ return 1U + sizeof...(Args); }
or if I redefine onion
in this way或者如果我以这种方式重新定义onion
template <typename ... O>
struct onion;
both clang++ and g++ compile without errors. clang++ 和 g++ 编译时都没有错误。
This looks like a compiler bug in clang 3.5.这看起来像是 clang 3.5 中的编译器错误。 If I run the code with the trunk version it compiles nicely.如果我使用主干版本运行代码,它会很好地编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.