6
#include <iostream>
#include <tuple>
int main(){

auto bt=std::make_tuple(std::tuple<>(),std::tuple<std::tuple<>>()); //Line 1
auto bt2=std::make_tuple(std::tuple<>(),std::tuple<>());             //Line 2
}

为什么第 1 行出现编译错误,而第 2 行编译正常?(在 Gcc 和 Clang 中测试)

有可能的解决方法吗?

铿锵的错误信息

/usr/include/c++/4.6/tuple:150:50: error: ambiguous conversion from derived class 'std::_Tuple_impl<0, std::tuple<>,
      std::tuple<std::tuple<> > >' to base class 'std::_Head_base<0, std::tuple<>, true>':
    struct std::_Tuple_impl<0, class std::tuple<>, class std::tuple<class std::tuple<> > > -> _Tuple_impl<0UL + 1, class std::tuple<class std::tuple<> > > -> _Head_base<1UL, class std::tuple<class std::tuple<> >, std::is_empty<class tuple<class tuple<> > >::value> -> class std::tuple<class std::tuple<> > -> _Tuple_impl<0, class std::tuple<> > -> _Head_base<0UL, class std::tuple<>, std::is_empty<class tuple<> >::value>
    struct std::_Tuple_impl<0, class std::tuple<>, class std::tuple<class std::tuple<> > > -> _Head_base<0UL, class std::tuple<>, std::is_empty<class tuple<> >::value>
      _Head&            _M_head()       { return _Base::_M_head(); }
                                                 ^~~~~
/usr/include/c++/4.6/tuple:173:33: note: in instantiation of member function 'std::_Tuple_impl<0, std::tuple<>,
      std::tuple<std::tuple<> > >::_M_head' requested here
        _Base(std::forward<_Head>(__in._M_head())) { }
                                       ^
/usr/include/c++/4.6/tuple:334:9: note: in instantiation of member function 'std::_Tuple_impl<0, std::tuple<>,
      std::tuple<std::tuple<> > >::_Tuple_impl' requested here
      : _Inherited(static_cast<_Inherited&&>(__in)) { }
        ^
gcc_bug.cpp:5:10: note: in instantiation of member function
      'std::tuple<std::tuple<>, std::tuple<std::tuple<> > >::tuple' requested here
        auto bt=std::make_tuple(std::tuple<>(),std::tuple<std::tuple<>>());
                ^
1 error generated.
4

2 回答 2

12

看起来您在 libstdc++ 中发现了一个错误!(此代码与 libc++ 一起工作)。简化的测试用例:

#include <tuple>

int main(){
    auto b = std::tuple<std::tuple<std::tuple<>>>{};
}

问题是由于如何std::tuple在 libstdc++ 中实现的。元组实现使用具有多重继承的“递归”。你可以认为tuple<X, Y, Z>继承自Xtuple<Y, Z>。这意味着tuple<tuple<>>将从两者继承,tuple<>并且tuple<>将导致模棱两可的基本错误。当然真正的问题不是这样的,因为tuple<tuple<>>不会产生任何错误。

导致错误的真正实现是这样的:

template<size_t _Idx, typename _Head>
struct _Head_base : public _Head
{};

template<size_t _Idx, typename... _Elements>
struct _Tuple_impl;

template<size_t _Idx>
struct _Tuple_impl<_Idx> {};

template<size_t _Idx, typename _Head, typename... _Tail>
struct _Tuple_impl<_Idx, _Head, _Tail...>
    : public _Tuple_impl<_Idx + 1, _Tail...>,
      private _Head_base<_Idx, _Head>
{
    typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
    constexpr _Tuple_impl() = default;
    constexpr _Tuple_impl(_Tuple_impl&& __in) : _Inherited(std::move(__in)) {}
};

template<typename... _Elements>
struct tuple : public _Tuple_impl<0, _Elements...> {};

当我们实例化tuple<tuple<tuple<>>>时,我们得到这个继承层次结构:

libstdc++中<code>tuple<tuple<tuple<>>></code>的继承图

我们看到这_Tuple_impl<1>可以通过两条不同的路径到达。这还不是问题,问题出在移动构造函数中,它调用了_Tuple_impl<1>. 你想要哪个_Tuple_impl<1>?编译器不知道,所以它选择了放弃。

(在您的情况下,这是因为_Head_base<0, tuple<>>您正在实例化tuple<tuple<>, tuple<tuple<>>>,但原理是相同的。)


为什么 libc++ 没有同样的问题?主要原因有两个:

  1. tuple<T...>在 libc++ 中使用组合而不是继承来引用__tuple_impl<...>.
  2. 结果,空基类优化__tuple_leaf<tuple<tuple<>>>不会启动,即__tuple_leaf<tuple<tuple<>>>不会继承自tuple<tuple<>>
  3. 因此,不会发生不明确的基类问题。
  4. (正如@mitchnull 提到的,每个碱基都是唯一的,但这不是主要区别。)

libc++中<code>tuple<tuple<tuple<>>></code>的继承图

正如我们在上面看到的,如果tuple<...>使用继承而不是组合,OPtuple<tuple<>, tuple<tuple<>>>仍然会继承__tuple_leaf<0, tuple<>>两次,这可能是一个问题。

于 2012-06-12T15:09:21.867 回答
0

顺便说一下,对于那些必须使用 gcc 的人,让我给你一个快速而肮脏的修复(对于 4.8.0,已经提交了错误报告):

解决方案是在元组实现中对 __empty_not_final 进行小修改,以防止 tuple<> 类型的空基优化:

template<typename _Tp>
    using __empty_not_final
      = typename conditional<__is_final(_Tp)||is_same<_Tp,tuple<>>::value,
false_type, is_empty<_Tp>>::type;

代替

template<typename _Tp>
    using __empty_not_final
      = typename conditional<__is_final(_Tp), false_type, is_empty<_Tp>>::type;

(注意,这只是 tuple<> 类型的临时解决方案,它并没有解决 KennyTM 描述的真正问题,即struct A{}; auto d = std::tuple<std::tuple<std::tuple<A, A>, A>, A>{};仍然无法编译)

于 2012-06-12T17:28:53.710 回答