g++和clang++具有不同的容器行为
g++ and clang++ different behaviour with variadic container
为了练习C++11,我正在使用可变模板。
特别是,我正在使用一种递归可变容器类(onion
)和一个返回模板类型数(func()
)的函数。
我遇到一个案例,clang++(3.5.0)无法编译,而g++(4.9.2)编译并运行时没有问题。
我把它简化为
#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)给了我以下编译器错误
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)编译无问题,运行时输出3
。
我的问题是:谁是对的?
clang++,给出错误,还是g++,编译?
我补充说,如果我用这种方式重写func()
template <typename X, typename ... Args>
std::size_t func (onion<X, Args...> const &)
{ return 1U + sizeof...(Args); }
或者如果我以这种方式重新定义onion
template <typename ... O>
struct onion;
clang++和g++编译都没有错误。
这看起来像clang 3.5中的一个编译器错误。如果我用trunk版本运行代码,它编译得很好。
相关文章:
- 奇怪的结构&GCC&clang(void*返回类型)
- 数据成员SFINAE的C++17测试:gcc vs clang
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- Clang bug?使用指针作为模板参数
- clang整洁10忽略了我的NOLINT命令
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 在clang++预处理器中确定gcc工具链版本
- 为什么 Clang 不允许"and"作为函数名称?
- 带有 -stdlib=libc++ 的 clang++ 9.0.1 找不到<optional>
- clang格式:宏的缩进
- CLANG 编译器 说:变量"PTR"可能未初始化
- clang格式:禁用排序包含
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 循环展开 - G++ 与 Clang++
- 如何使用Clang/GCC在Mac上为C/C++设置VSCode
- 一位朋友将模板函数缩写为clang和gcc
- 类模板参数推导-clang和gcc不同