gpt4 book ai didi

c++ - R 值似乎无法为未命名的临时对象提供不完整的支持,或者我在这里遗漏了什么?

转载 作者:行者123 更新时间:2023-11-30 03:31:42 25 4
gpt4 key购买 nike

R 值似乎无法为未命名的临时对象提供不完整的支持,或者我在这里遗漏了什么?


C++11 为实现 move 语义的右值提供了出色的支持,有助于将昂贵的分配和复制周期转换为常数时间内快速且廉价的 move ,类似于 move 引用。但是 C++11 来晚了,到那时我已经有了一个完全开发的解决方案来解决代价高昂的未命名临时对象问题,使用下面概述的基于类的解决方案。

只有当我最近尝试用“现代”C++11 move 构造函数替换我的解决方案时,我才发现右值管理没有涵盖基于类的解决方案涵盖的重要情况。一个代表性的例子是表达式 A + B。当 A 是一个未命名的临时(右值)时,A += B 的就地实现是合适的,当 A 不是一个未命名的临时(左值)时,A + B 计算一个新的结果。但是 C+11 右值支持似乎只解决右参数右值,而不是左右值。

通过扩展,此限制会影响基类型上的所有其他运算符和函数,这些运算符和函数可以从适当的时候将 *this 视为右值而受益。请注意,当 A 是左值且 B 是右值时,A + B 甚至可以计算为 B += A。完整解决方案的好处通常可以更多地应用于 *this 右值而不是右参数右值。如果 C++11 在这里只提供了一半的解决方案,那么下面的基于类的解决方案在很多方面仍然非常优越。我在这里遗漏了什么吗?


因此,让我们从 S 值的类派生一个未命名的临时 T 类,向 S 和 T 添加适当的构造函数、赋值、运算符和函数,然后用 T 替换 S 作为返回 S 的所有函数和运算符的返回类型结果。有了这个,我们得到了与右值相同的 move 语义,加上对额外函数和运算符的支持,这些函数和运算符可以就地对未命名的临时值进行更快的操作。

class S {                      // S is a sample base type to extend
protected:
mutable char* p; // mutable pointer to storage
mutable int length; // mutable current length
mutable int size; // mutable current size
public:
~S ( ); // S destructor
S (char* s); // construct from data
S (const S& s); // from another S
S (const T& s); // construct from an unnamed temporary

T& result ( ) { return (T&)*this; } // cast *this into a T& (an equivalent to std::move (*this))
S& take (S& s); // free *this, move s to *this, put s in empty/valid state

S& operator= (const S& s); // copy s to *this
S& operator= (const T& s); // assign from unnamed temporary using take ( )

S& operator+= (const S& v); // add v to *this in-place
S& operator-= (const S& v); // subtract v from *this in-place
S& operator<<= (Integer shift); // shift *this in-place
S& operator>>= (Integer shift);

T operator+ (const S& v); // add v to *this and return a T
T operator- (const S& v); // subtract v from *this and return a T
etc...
};

class T : public S { // T is an unnamed temporary S
private:
T& operator= (const T& s); // no public assignments
void* operator new (size_t size); // don't define -- no heap allocation
void operator delete (void* ptr);
public:
T (char* s) : S (s) { }; // create a new temporary from data
T (const S& s) : S (s) { }; // copy a new temporary from a non-temporary
T (const T& s) : S (s) { }; // move a temporary to new temporary

T operator<< (int shift) const { return ((S&)*this <<= shift).result ( ); }
T operator>> (int shift) const { return ((S&)*this >>= shift).result ( ); }

T operator+ (const S& v) const { return ((S&)*this += v).result ( ); }
T operator- (const S& v) const { return ((S&)*this -= v).result ( ); }
};

请注意,自 2001 年以来,此方法已在各种综合数据类型(包括字符串、数组、大整数等)中证明了其正确性和有效性,因此它可以在不引用 C++11 功能的情况下工作,并且依赖于没有未定义的语言特性。

最佳答案

您的假设似乎是错误的。 C++ 支持左值或右值。

有两种方法可以做到这一点。

struct noisy {
noisy() { std::cout << "ctor()\n"; };
noisy(noisy const&) { std::cout << "ctor(const&)\n"; };
noisy(noisy &&) { std::cout << "ctor(&&)\n"; };
noisy& operator=(noisy const&) { std::cout << "asgn(const&)\n"; return *this; };
noisy& operator=(noisy &&) { std::cout << "asgn(&&)\n"; return *this; };
~noisy() { std::cout << "dtor\n"; };
};
struct Bob:noisy {
int val = 0;
Bob(int x=0):val(x) {}
Bob(Bob&&)=default;
Bob(Bob const&)=default;
Bob& operator=(Bob&&)=default;
Bob& operator=(Bob const&)=default;
friend Bob operator+( Bob lhs, Bob const& rhs ) {
lhs += rhs;
return lhs;
}
friend Bob& operator+=( Bob& lhs, Bob const& rhs ) {
lhs.val += rhs.val;
return lhs;
}
friend Bob operator+=( Bob&& lhs, Bob const& rhs ) {
lhs += rhs; // uses & overload above
return std::move(lhs);
}
};

Bob 使用友元运算符基本上可以做您想做的事。

这是我的首选解决方案,友元运算符比成员运算符对称得多。

struct Alice:noisy {
int val = 0;
Alice(int x=0):val(x) {}
Alice(Alice&&)=default;
Alice(Alice const&)=default;
Alice& operator=(Alice&&)=default;
Alice& operator=(Alice const&)=default;
Alice operator+( Alice const& rhs ) const& {
return Alice(*this) + rhs;
}
Alice operator+( Alice const& rhs ) && {
*this += rhs;
return std::move(*this);
}
Alice& operator+=( Alice const& rhs )& {
val += rhs.val;
return *this;
}
Alice operator+=( Alice const& rhs )&& {
*this += rhs; // uses & overload above
return std::move(*this);
}
};

Alice 使用成员函数来做同样的事情。它存在成员函数优于友元运算符的常见问题。

注意 &&&const& 成员函数参数之后的使用。在不经意的讨论中,这被称为“对 *this 的右值引用”功能。它允许您根据正在处理的对象的 r/l value-ness 选择哪个重载。

测试代码:

Bob bob;
Bob b2 = Bob{3}+bob;

Alice alice;
Alice a2 = Alice{3}+alice;

Live example .

在这两种情况下,都不会复制任何对象。

请注意,我假设加法是不对称的(尽管使用 int 作为状态)。如果是,您可以执行另一种效率,其中 lhs 是非右值而 rhs 是。

关于c++ - R 值似乎无法为未命名的临时对象提供不完整的支持,或者我在这里遗漏了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44010550/

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