2

以下代码https://godbolt.org/z/X0na5H无法编译以下代码的右值版本。我使用了我在这里找到的 BOOST_FOREACH 可扩展性指南

简而言之,我似乎无法让 boost_foreach 使用非复制右值。这甚至可能吗?

#include <boost/optional.hpp>
#include <boost/foreach.hpp>
#include <vector>

 struct IdentityOp : std::vector<int>, boost::noncopyable {
    IdentityOp(std::vector<int> && v):std::vector<int>(std::move(v)){}

};

namespace boost { namespace foreach
{

    template <>
    struct is_noncopyable< IdentityOp >
      : mpl::true_
    {
    };
}}

#define BGS_TEST_LVALUE

// At global scope...
inline boost::mpl::true_ *
boost_foreach_is_noncopyable( IdentityOp *&, boost::foreach::tag )
{
    return 0;
}

int main(){
    std::vector<int> v;

    #ifdef BGS_TEST_LVALUE
    // LVALUE VERSION
    IdentityOp op(std::move(v));
    BOOST_FOREACH(const auto & a, op)
    #else
    // RVALUE VERSION
    BOOST_FOREACH(const auto & a, IdentityOp(std::move(v)))
    #endif
    {}

}

显示问题。如果我定义BGS_TEST_LVALUE然后代码编译。如果我没有定义它,我会得到错误。

In file included from <source>:2:

/celibs/boost_1_70_0/boost/foreach.hpp: In instantiation of 'boost::foreach_detail_::auto_any<T> boost::foreach_detail_::contain(const T&, mpl_::true_*) [with T = IdentityOp; mpl_::true_ = mpl_::bool_<true>]':

<source>:34:5:   required from here

/celibs/boost_1_70_0/boost/foreach.hpp:632:25: error: use of deleted function 'boost::foreach_detail_::auto_any<IdentityOp>::auto_any(boost::foreach_detail_::auto_any<IdentityOp>&&)'

  632 |     return auto_any<T>(t);

      |                         ^

/celibs/boost_1_70_0/boost/foreach.hpp:253:8: note: 'boost::foreach_detail_::auto_any<IdentityOp>::auto_any(boost::foreach_detail_::auto_any<IdentityOp>&&)' is implicitly deleted because the default definition would be ill-formed:

  253 | struct auto_any : auto_any_base

      |        ^~~~~~~~

/celibs/boost_1_70_0/boost/foreach.hpp:253:8: error: call of overloaded 'IdentityOp(IdentityOp)' is ambiguous

<source>:8:5: note: candidate: 'IdentityOp::IdentityOp(std::vector<int>&&)'

    8 |     IdentityOp(std::vector<int> && v):std::vector<int>(std::move(v)){}

      |     ^~~~~~~~~~

<source>:7:9: note: candidate: 'IdentityOp::IdentityOp(const IdentityOp&)' <deleted>

    7 |  struct IdentityOp : std::vector<int>, boost::noncopyable {

      |         ^~~~~~~~~~

<source>:7:9: note: candidate: 'IdentityOp::IdentityOp(IdentityOp&&)' <deleted>

In file included from <source>:2:

/celibs/boost_1_70_0/boost/foreach.hpp: In instantiation of 'boost::foreach_detail_::auto_any<T>::auto_any(const T&) [with T = IdentityOp]':

/celibs/boost_1_70_0/boost/foreach.hpp:632:12:   required from 'boost::foreach_detail_::auto_any<T> boost::foreach_detail_::contain(const T&, mpl_::true_*) [with T = IdentityOp; mpl_::true_ = mpl_::bool_<true>]'

<source>:34:5:   required from here

/celibs/boost_1_70_0/boost/foreach.hpp:256:15: error: use of deleted function 'IdentityOp::IdentityOp(const IdentityOp&)'

  256 |       : item(t)

      |               ^

<source>:7:9: note: 'IdentityOp::IdentityOp(const IdentityOp&)' is implicitly deleted because the default definition would be ill-formed:

    7 |  struct IdentityOp : std::vector<int>, boost::noncopyable {

      |         ^~~~~~~~~~

<source>:7:9: error: use of deleted function 'boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)'

In file included from /celibs/boost_1_70_0/boost/noncopyable.hpp:15,

                 from /celibs/boost_1_70_0/boost/foreach.hpp:72,

                 from <source>:2:

/celibs/boost_1_70_0/boost/core/noncopyable.hpp:49:7: note: declared here

   49 |       noncopyable( const noncopyable& ) = delete;

      |       ^~~~~~~~~~~

Compiler returned: 1

https://godbolt.org/z/X0na5H

4

0 回答 0