简体   繁体   English

g++ 和 clang++ 与可变参数容器的不同行为

[英]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.

相关问题 g ++和clang ++使用指向可变参数模板函数的指针的不同行为 - g++ and clang++ different behaviour with pointer to variadic template functions g ++和clang ++不同的行为推导可变参数模板`auto`值 - g++ and clang++ different behaviour deducing variadic template `auto` values g ++和clang ++使用静态成员的递归初始化的不同行为 - g++ and clang++ different behaviour with recursive initialization of a static member g ++和clang ++使用变量模板和SFINAE的不同行为 - g++ and clang++ different behaviour with variable template and SFINAE g ++和clang ++使用运算符&lt;()重载的不同行为 - g++ and clang++ different behaviour with operator<() overloading g ++和clang ++使用自动参数的模板特化的不同行为 - g++ and clang++ different behaviour with template specialization for auto argument 带有集成模板参数的g ++和clang ++不同的行为 - g++ and clang++ different behaviour with integral template parameter g ++和clang ++与流输入和无符号整数的不同行为 - g++ and clang++ different behaviour with stream input and unsigned integer g ++和clang ++与SFINAE和SFINAE失败的不同行为 - g++ and clang++ different behaviour with SFINAE and SFINAE failure 空包装的Functor可变参数模板包装扩展在clang ++和g ++中给出了不同的结果 - Functor variadic template pack expansion for empty pack gives different results in clang++ and g++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM