gpt4 book ai didi

c++ - 使用包含右值引用的成员元组构造类

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:26:38 25 4
gpt4 key购买 nike

当试图构建一个类,该类应该包含通过调用 std::forward_as_tuple 创建的元组时,我在使用 clang(187537) 和 libc++ 进行编译时遇到了以下错误:

/usr/include/c++/v1/tuple:329:11: error: rvalue reference to type 'int' cannot
bind to lvalue of type 'int'
: value(__t.get())
^ ~~~~~~~~~
/usr/include/c++/v1/tuple:447:8: note: in instantiation of member function
'std::__1::__tuple_leaf<0, int &&, false>::__tuple_leaf' requested here
struct __tuple_impl<__tuple_indices<_Indx...>, _Tp...>
^
tuple.cpp:31:5: note: in instantiation of function template specialization
'make_foo2<int>' requested here
make_foo2(1 + 1);
^
In file included from tuple.cpp:2:
/usr/include/c++/v1/tuple:330:10: error: static_assert failed "Can not copy a
tuple with rvalue reference member"
{static_assert(!is_rvalue_reference<_Hp>::value, "Can not copy ...

我能够通过以不同方式声明返回类型来解决上述错误,但是,根据我的理解,它应该具有相同的语义,所以我不希望它停止错误。在下面的代码中,make_foo 是不会出错的解决方法,而 make_foo2 会导致上述错误。我能够使用 gcc 4.8.1 和 coliru 的 clang 版本成功编译这两个版本。 .

#include <utility>
#include <tuple>

template<class Tuple>
struct foo
{
Tuple t;
foo(Tuple &&t) : t(std::move(t)) { }
};

template<class... Args>
using ForwardedTuple = decltype(std::forward_as_tuple(std::forward<Args>(std::declval<Args>())...));

template<class... Args>
foo<ForwardedTuple<Args...>> make_foo(Args&&... args)
{
return {std::forward_as_tuple(std::forward<Args>(args)...)};
}

template<class... Args>
auto make_foo2(Args&& ...args) ->
decltype(foo<decltype(std::forward_as_tuple(std::forward<Args>(args)...))>(std::forward_as_tuple(std::forward<Args>(args)...)))
{
return foo<decltype(std::forward_as_tuple(std::forward<Args>(args)...))>(std::forward_as_tuple(std::forward<Args>(args)...));
}

int main()
{
make_foo(1 + 1);

make_foo2(1 + 1);
}

上面的make_foo函数有什么区别,make_foo2是不是不正确?

谢谢。

最佳答案

看起来你从 make_foo2 返回了 foo<>。但是 foo 没有生成移动构造函数(编译器不会生成它)。因此复制构造函数被调用,编译因此失败。

关于c++ - 使用包含右值引用的成员元组构造类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18369640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com