【问题标题】:Calling `std::get` on `std::tuple` with elements deriving from `std::tuple` - ill-formed?使用源自 `std::tuple` 的元素在`std::tuple` 上调用`std::get` - 格式错误?
【发布时间】:2017-02-01 21:55:03
【问题描述】:
struct Y { };
struct X : std::tuple<Y> { };

int main()
{
    std::get<0>(std::make_tuple(X{}));
}

on wandbox


当使用 libc++ 时,上面的代码在 clang++ 下编译并按预期工作。

使用 libstdc++ 时,上述代码无法同时使用clang++g++ 进行编译,并出现以下错误:

include/c++/7.0.1/tuple:1302:36: 
error: no matching function for call to ‘__get_helper<0>(std::tuple<X>&)’
    { return std::__get_helper<__i>(__t); }
            ~~~~~~~~~~~~~~~~~~~~~~^~~~~

include/c++/7.0.1/tuple:1290:5: 
note: candidate: template<long unsigned int __i, class _Head, class ... _Tail> 
                constexpr _Head& std::__get_helper(std::_Tuple_impl<_Idx, _Head, _Tail ...>&)
    __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
    ^~~~~~~~~~~~

include/c++/7.0.1/tuple:1290:5: 
note:   template argument deduction/substitution failed:
include/c++/7.0.1/tuple:1302:36: 
note:   ‘std::_Tuple_impl<0, _Head, _Tail ...>’ is an ambiguous base class of ‘std::tuple<X>’
    { return std::__get_helper<__i>(__t); }
            ~~~~~~~~~~~~~~~~~~~~~~^~~~~

include/c++/7.0.1/tuple:1295:5: 
note: candidate: template<long unsigned int __i, class _Head, class ... _Tail> 
                constexpr const _Head& std::__get_helper(const std::_Tuple_impl<_Idx, _Head, _Tail ...>&)
    __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
    ^~~~~~~~~~~~

include/c++/7.0.1/tuple:1295:5: 
note:   template argument deduction/substitution failed:
include/c++/7.0.1/tuple:1302:36: 
note:   ‘const std::_Tuple_impl<0, _Head, _Tail ...>’ is an ambiguous base class of ‘std::tuple<X>’
    { return std::__get_helper<__i>(__t); }
            ~~~~~~~~~~~~~~~~~~~~~~^~~~~

当元组元素在调用std::get 时从std::tuple 派生时,libstdc++std::tuple 的基于继承的实现似乎会导致歧义。我倾向于认为这是 libstdc++ 中的实现缺陷 - 是这样吗?或者标准中有什么东西会使代码 sn-p 格式错误?

【问题讨论】:

标签: c++ c++11 language-lawyer libstdc++ stdtuple


【解决方案1】:

正如cmets中T.C.所说,这是由known bug #71096引起的。

【讨论】:

    猜你喜欢
    • 2020-07-07
    • 2019-03-20
    • 2019-06-15
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2010-11-14
    相关资源
    最近更新 更多