g ++和clang ++不同的行为与可变容器 [英] g++ and clang++ different behaviour with variadic container

查看:619
本文介绍了g ++和clang ++不同的行为与可变容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了练习C ++ 11,我在使用可变参数模板。

In order to practice with C++11, I'm playing with variadic templates.

特别是,我使用一种递归可变容器类( onion )和返回模板类型数量的函数( func())。

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()).

我遇到一个情况,即clang ++(3.5.0)无法编译,而g ++(4.9.2)编译和运行没有问题。

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.

我简化了它如下

#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

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

g++ (4.9.2) compiles without problem and, running, output 3.

我的问题是:谁是对的?

My question is: who's right?

clang ++,或者g ++,编译?

clang++, giving an error, or g++, compiling?

我补充说,如果我以这种方式重写 func()

I add that if I rewrite func() in this way

template <typename X, typename ... Args>
std::size_t func (onion<X, Args...> const &)
 { return 1U + sizeof...(Args); }

或如果我在此重新定义 onion

or if I redefine onion in this way

template <typename ... O>
struct onion;

两个clang ++和g ++编译没有错误。

both clang++ and g++ compile without errors.

推荐答案

这看起来像是clang 3.5中的编译器错误。
如果我用中继版本运行代码,它编译得很好。

This looks like a compiler bug in clang 3.5. If I run the code with the trunk version it compiles nicely.

这篇关于g ++和clang ++不同的行为与可变容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆