当将标准库转换应用于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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句