2

当试图构建一个应该包含通过调用创建的元组的类时,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 和colliru的 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不正确?

谢谢。

4

1 回答 1

1

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

于 2013-08-22T02:01:58.047 回答