调用`std::tuple`中继承自`std::tuple`的元素的`std::get`是否不合法?

7
struct Y { };
struct X : std::tuple<Y> { };

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

在wandbox上,以上代码在使用libc++时,可以如预期地编译和工作。

当使用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); }
            ~~~~~~~~~~~~~~~~~~~~~~^~~~~

似乎 libstdc++ 的基于继承的 std::tuple 实现会在调用 std::get 时造成元素从 std::tuple 继承的歧义。我倾向于认为这是 libstdc++ 的实现缺陷 - 是这样吗?还是标准中有什么东西会使代码片段不合法?


7
这是一个关于GCC编译器的漏洞报告,该漏洞会在一些特定情况下导致代码生成错误。具体而言,当使用某些类型的内联汇编代码时,会发生这种错误。报告提供了一些详细信息和示例代码,以帮助开发人员更好地理解问题并进行修复。 - T.C.
1个回答

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接