std :: transform中的二进制运算符,带有unique_ptr向量

时髦的

当将标准库转换应用于unique_ptr向量时,我会感到困惑。我定义了一个二进制仿函数addScalar,它接受2个对unique_ptr的const引用,并返回对unique_ptr的const引用,以避免复制(unique_ptr禁止使用)。

然后我尝试在std :: transform中使用它,但是尽管我已采取一切预防措施避免unique_ptr复制,但unique_ptr似乎根本无法进行二进制操作...

有谁知道如何在std :: unique_ptr中使用std :: transform吗?还是我必须通过for循环遍历向量并“手动”执行加法?我也想知道是否可以unique_ptr<const Scalar>在我的函子中使用

这是我的课:

#include "space.h"
#include "scalar.h"
#include <vector>
#include <algorithm>
#include <memory>

using std::vector;
using std::ostream;
using std::unique_ptr;

class addScalar
{
   public:
      unique_ptr<Scalar> const& operator()(unique_ptr<Scalar> const& scal1, unique_ptr<Scalar> const& scal2)
      {
         *scal1 += *scal2;
         return scal1;
      };
};

class Tensor4D
{
   public:
      Tensor4D(Space& space_in, int ncomp);
      Tensor4D(const Tensor4D& tens);
      Tensor4D& operator=(const Tensor4D& tens);
      size_t size() const {return comp.size();};
      ~Tensor4D();
   protected:
      Space* const space;
      vector<unique_ptr<Scalar>> comp;
   public:
      Tensor4D& operator+=(const Tensor4D& tens);
};

这是operator + =的实现:

Tensor4D& Tensor4D::operator+=(const Tensor4D& tens)
{
   assert(comp.size() == tens.comp.size());
   transform(tens.comp.begin(), tens.comp.end(), comp.begin(), tens.comp.begin(), addScalar());
   return *this;
}

我收到以下丑陋的编译器错误:

/usr/include/c++/4.8/bits/stl_algo.h: In instantiation of ‘_OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation) [with _IIter1 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Scalar>*, std::vector<std::unique_ptr<Scalar> > >; _IIter2 = __gnu_cxx::__normal_iterator<std::unique_ptr<Scalar>*, std::vector<std::unique_ptr<Scalar> > >; _OIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Scalar>*, std::vector<std::unique_ptr<Scalar> > >; _BinaryOperation = addScalar]’:
tensor4D.C:44:94:   required from here
/usr/include/c++/4.8/bits/stl_algo.h:4965:12: error: no match for ‘operator=’ (operand types are ‘const std::unique_ptr<Scalar>’ and ‘const std::unique_ptr<Scalar>’)
  *__result = __binary_op(*__first1, *__first2);
        ^
/usr/include/c++/4.8/bits/stl_algo.h:4965:12: note: candidates are:
In file included from /usr/include/c++/4.8/memory:81:0,
             from /home/gmartinon/Kadath/C++/Include/scalar.h:27,
             from tensor4D.h:5,
             from tensor4D.C:1:
/usr/include/c++/4.8/bits/unique_ptr.h:190:7: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Tp, _Dp>&&) [with _Tp = Scalar; _Dp = std::default_delete<Scalar>]
   operator=(unique_ptr&& __u) noexcept
   ^
/usr/include/c++/4.8/bits/unique_ptr.h:190:7: note:   no known conversion for argument 1 from ‘const std::unique_ptr<Scalar>’ to ‘std::unique_ptr<Scalar>&&’
/usr/include/c++/4.8/bits/unique_ptr.h:203:2: note: template<class _Up, class _Ep> typename std::enable_if<std::__and_<std::is_convertible<typename std::unique_ptr<_Up, _Ep>::pointer, typename std::unique_ptr<_Tp, _Dp>::_Pointer::type>, std::__not_<std::is_array<_Up> > >::value, std::unique_ptr<_Tp, _Dp>&>::type std::unique_ptr<_Tp, _Dp>::operator=(std::unique_ptr<_Up, _Ep>&&) [with _Up = _Up; _Ep = _Ep; _Tp = Scalar; _Dp = std::default_delete<Scalar>]
  operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
  ^
/usr/include/c++/4.8/bits/unique_ptr.h:203:2: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/algorithm:62:0,
             from tensor4D.h:8,
             from tensor4D.C:1:
/usr/include/c++/4.8/bits/stl_algo.h:4965:12: note:   types ‘std::unique_ptr<_Tp, _Dp>’ and ‘const std::unique_ptr<Scalar>’ have incompatible cv-qualifiers
  *__result = __binary_op(*__first1, *__first2);
        ^
In file included from /usr/include/c++/4.8/memory:81:0,
             from /home/gmartinon/Kadath/C++/Include/scalar.h:27,
             from tensor4D.h:5,
             from tensor4D.C:1:
/usr/include/c++/4.8/bits/unique_ptr.h:211:7: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(std::nullptr_t) [with _Tp = Scalar; _Dp = std::default_delete<Scalar>; std::nullptr_t = std::nullptr_t]
       operator=(nullptr_t) noexcept
       ^
/usr/include/c++/4.8/bits/unique_ptr.h:211:7: note:   no known conversion for argument 1 from ‘const std::unique_ptr<Scalar>’ to ‘std::nullptr_t’
/usr/include/c++/4.8/bits/unique_ptr.h:274:19: note: std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Scalar; _Dp = std::default_delete<Scalar>] <near match>
   unique_ptr& operator=(const unique_ptr&) = delete;
               ^
/usr/include/c++/4.8/bits/unique_ptr.h:274:19: note:   no known conversion for implicit ‘this’ parameter from ‘const std::unique_ptr<Scalar>*’ to ‘std::unique_ptr<Scalar>*’
克里斯·德鲁

addScalar的返回类型将分配给一个,unique_ptr<Scalar>因此它没有返回const引用,因为unique_ptr没有副本分配。因此,您将必须返回按值来调用移动分配。

为了避免构造一个新值,Scalar您可以使用astd:move_iterator移入addScalar,然后使用move Assign覆盖从值移出的值:

class addScalar 
{
public:
    unique_ptr<Scalar> operator()(unique_ptr<Scalar> scal1,
                                  unique_ptr<Scalar> const& scal2) {
      *scal1 += *scal2;
      return scal1;
    };
};

Tensor4D& Tensor4D::operator+=(const Tensor4D& tens)
{
   assert(comp.size() == tens.comp.size());
   transform(make_move_iterator(comp.begin()), make_move_iterator(comp.end()),
               tens.comp.begin(), comp.begin(), addScalar());
   return *this;
}

Andrey提出了一个很好的观点,目前尚不清楚是否根据标准严格允许这样做。我将其留给语言律师。另请参阅此答案

现场演示。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在带有 std::unique_ptr 的类赋值运算符中,

std :: stringstream具有二进制数据的流运算符

为什么在std :: unique_ptr中没有运算符<<?

将 std::map 写入/读取到二进制文件需要运算符

为什么std :: unique_ptr具有相等运算符?

错误C2678:二进制'<<':未找到采用'const std :: ofstream'类型的左操作数的运算符(或者没有可接受的转换)

错误C2679:二进制'<<':未找到采用'std :: string'类型的右侧操作数的运算符(或者没有可接受的转换)

错误C2679:“二进制'<<':未找到采用'std :: string'类型的右侧操作数的运算符(或没有可接受的转换)”

二进制'[':'std :: initializer_list <const char *>'未定义此运算符或未转换为预定义运算符可接受的类型

错误C2676二进制'<<':'std :: ostream'未定义此运算符或未转换为预定义运算符可接受的类型

错误C2679:二进制'<<':找不到运算符,该运算符采用类型为'std :: string'的右侧操作数

二进制 '<<' : 未找到采用类型为 'std::basic_ostream<char, std::char_traits<char>>' 的左操作数的运算符

为什么 std::unique_ptr 与赋值运算符不兼容?

错误 C2679:二进制“<<”:未找到采用“std::vector<char,std::allocator<char>>”类型的右侧操作数的运算符(或者没有接受

当unique_ptr在向量中时,对std :: unique_ptr <>拥有的对象的引用/ ptr是否安全?

二进制搜索std:map

带有std :: unique_ptr的clang错误

铛,返回带有类型转换的std :: unique_ptr

使用带有分配器的std :: unique_ptr

带有 STL 容器的 C++ std::unique_ptr

std ::查找具有重载==运算符的对象的向量

std向量与运算符不匹配==

在std :: unique_ptr的向量上使用make_transform_iterator获取const指针

xbase中带有二进制运算符的Java表达式不起作用

根据callable的签名自动选择一元与二进制std :: transform函数重载

为什么对排序后的向量进行二进制搜索比std :: set查找要慢?

std :: unique_ptr :: release()与std :: move()

std :: unique_ptr与std :: map

二进制运算符或在TSQL中?