我注意到 libstdc++ 的 std::ignore
实现采用了 const T&
参数,它不能绑定(bind)到 volatile 右值。因此,以下代码无法编译:
#include <tuple>
#include <utility>
struct C {};
using VC = C volatile;
int main() {
std::tuple<VC> t;
std::tie(std::ignore) = std::move(t);
}
( http://coliru.stacked-crooked.com/a/7bfc499c1748e59e )
这是否违反了标准,或者是否存在导致这种未定义行为的条款?
最佳答案
我不是语言律师,所以我将尽可能直接地回答这个问题。
ignore
可在 tuple
的概要中找到在 tuple.general
因此:
// [tuple.creation], tuple creation functions:
const unspecified ignore;
如您所见,libstdc++实现定义 ignore
像这样:
// A class (and instance) which can be used in 'tie' when an element
// of a tuple is not required
struct _Swallow_assign
{
template<class _Tp>
const _Swallow_assign&
operator=(const _Tp&) const
{ return *this; }
};
而 libc++版本定义如下:
template <class _Up>
struct __ignore_t
{
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY
const __ignore_t& operator=(_Tp&&) const {return *this;}
};
因此,它在 libc++ 中编译。现在定义 std::tie
可以在 [tuple.creation] 中找到:
Returns:
tuple<Types&...>(t...)
. When an argument int
isignore
, assigning any value to the corresponding tuple element has no effect.
这并没有说明 ignore
本身,所以我要把这归因于未指定的行为。您可以通过遗漏争辩说它是未定义行为,但这可能会夸大其词。
关于c++ - libstdc++ 拒绝将 volatile 右值分配给 std::ignore 是错误的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37932676/