首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用户定义对象的C++长算术表达式优化

用户定义对象的C++长算术表达式优化
EN

Stack Overflow用户
提问于 2020-03-11 11:46:05
回答 1查看 130关注 0票数 0

我在试着让操作员超负荷。我注意到,在执行长表达式时,编译器会在语句末尾(在;之后)创建临时代码并销毁它们。但是,当表达式变长或对象更大时,内存就会无缘无故地被占用(据我所知)。我想要的是像这样使用后销毁它们:

代码语言:javascript
复制
    Test m3 = m1 - m2*2 + 3*m1;
    /* Temporaries predicted from result, temporaries might 
    have different declarations like 'const Test temp = ...;'
    Test temp_m1 = m1*3;
    Test temp_m2 = m2*2;
    Test temp_m3 = m1 - temp_m2;
    Test m3 = temp_m3 + temp_m1;
    temp_m3.~Test();  //destroyed at the end of statement
    temp_m2.~Test();  //destroyed at the end of statement
    temp_m1.~Test();  //destroyed at the end of statement
           ||
           \/
    Test temp_m1 = m1*3;
    Test temp_m2 = m2*2;
    Test temp_m3 = m1 - temp_m2;
    temp_m2.~Test();  //destroyed after temp_m2 is used
    Test m3 = temp_m3 + temp_m1;
    temp_m3.~Test();  //destroyed after temp_m3 is used
    temp_m1.~Test();  //destroyed after temp_m1 is used
    */

我试过输入rvalue到操作符重载函数,但它不是作为临时值的工作。我注意到临时人员是通过move构造函数创建的,可以利用这一点,但这并不理想。

问题是:

  1. 是否有办法区分时间变量和正常变量?
  2. ,如果没有,是否有其他方法在使用后销毁它们?
  3. 在语句结束时销毁它们是否有任何意义?

Main:

代码语言:javascript
复制
#include <iostream>
#include <iomanip>
class Test {
public:
    Test(const Test& m);
    Test(unsigned int limit = 0, int value = 0);
    Test(Test&& m);
    ~Test();

    Test& operator=(const Test& m);
    Test& operator=(Test&& m);
    Test operator+(const Test& m) const;
    Test operator-(const Test& m) const;
    Test operator*(const int& scalar) const;
    friend std::ostream& operator<<(std::ostream& os, const Test& m);
    void reset();
    unsigned int limit_ = 0;
    std::unique_ptr<int[]> data_;
};
int main(){
    Test m2(2,2);
    Test m1(2,1);
    Test m3 = m1 - m2*2 + 3*m1;
    /* Temporaries predicted from result, temporaries might 
    have different declarations like 'const Test temp = ...;'
    Test temp_m1 = m1*3;
    Test temp_m2 = m2*2;
    Test temp_m3 = m1 - temp_m2;
    Test m3 = temp_m3 + temp_m1;
    temp_m3.~Test();
    temp_m2.~Test();
    temp_m1.~Test();
    */
    std::cout << "RESULT" << std::endl;
    std::cout << m1;
    std::cout << m2;
    std::cout << m3;
}

结果:

代码语言:javascript
复制
Constructor: [ 2     2    ]
Constructor: [ 1     1    ]

Multiplication operation:
Constructor: [ 0     0    ]
[ 1     1    ]
 * 3 = [ 3     3    ]
Move constructor: [ 3     3    ]   //temp_m1
Destructor: Empty


Multiplication operation:
Constructor: [ 0     0    ]
[ 2     2    ]
 * 2 = [ 4     4    ]
Move constructor: [ 4     4    ]   //temp_m2
Destructor: Empty


Subtraction operation:
Constructor: [ 0     0    ]
[ 1     1    ]
 - [ 4     4    ]
 = [ -3    -3   ]
Move constructor: [ -3    -3   ]   //temp_m3
Destructor: Empty


Addition operation:
Constructor: [ 0     0    ]
[ -3    -3   ]
 + [ 3     3    ]
 = [ 0     0    ]
Move constructor: [ 0     0    ]   //m3
Destructor: Empty

Destructor: [ -3    -3   ]   //temp_m3 destroyed at the end of statement

Destructor: [ 4     4    ]   //temp_m2 destroyed at the end of statement

Destructor: [ 3     3    ]   //temp_m1 destroyed at the end of statement

RESULT
[ 1     1    ]
[ 2     2    ]
[ 0     0    ]
Destructor: [ 0     0    ]

Destructor: [ 1     1    ]

Destructor: [ 2     2    ]


Process returned 0 (0x0)   execution time : 0.125 s

实施守则:

代码语言:javascript
复制
Test::~Test(){
        std::cout << "Destructor: " << *this << std::endl;
        data_.reset();
}
Test::Test(Test&& m){  //Move constructor
    std::cout << "Move constructor: ";
    data_ = std::move(m.data_);
    limit_ = m.limit_;
    m.reset();
    std::cout << *this;
}
Test::Test(const Test& m){  //Copy constructor
    std::cout << "Copy constructor: ";
    data_ = std::make_unique<int[]>(m.limit_);
    limit_ = m.limit_;
    for(unsigned int i=0; i<limit_ ; ++i) data_[i] = m.data_[i];
    std::cout << *this;
}
Test::Test(unsigned int limit, int value){    //Constructor
    std::cout << "Constructor: ";
    data_ = std::make_unique<int[]>(limit);
    limit_ = limit;
    if(limit == 0) return;
    for(unsigned int i=0; i<limit ; ++i) data_[i] = value;
    std::cout << *this;
}
Test& Test::operator=(const Test& m){ //Copy assignment
    std::cout << "Copy assignment: ";
    Test local_m = m;

    limit_ = local_m.limit_;
    data_ = std::move(local_m.data_);
    std::cout << *this << std::endl;
    return *this;
}
Test& Test::operator=(Test&& m){  //Move assignment
    std::cout << "Move assignment: ";
    Test local_m = std::move(m);

    limit_ = local_m.limit_;
    data_ = std::move(local_m.data_);
    std::cout << *this << std::endl;
    return *this;
}
Test Test::operator+(const Test& m) const{ //Addition
    std::cout << std::endl<< "Addition operation: " <<std::endl;
    if( limit_ != m.limit_){
        std::cout << "Addition error: not same dimensions, return Empty" <<std::endl;
        return Test();
    }
    if( limit_ == 0){
        std::cout << "Addition warning: Empty, return Empty" <<std::endl;
        return Test();
    }
    Test result(limit_);
    for ( unsigned int i = 0; i < limit_; ++i ) result.data_[i] = data_[i] + m.data_[i];
    std::cout << *this << " + " << m << " = " << result;
    return result;
}
Test Test::operator-(const Test& m) const{ //Subtraction
    std::cout << std::endl<< "Subtraction operation: " <<std::endl;
    if( limit_ != m.limit_){
        std::cout << "Subtraction error: not same dimensions, return Empty" <<std::endl;
        return Test();
    }
    if( limit_ == 0){
        std::cout << "Subtraction warning: Empty, return Empty" <<std::endl;
        return Test();
    }
    Test result(limit_);
    for ( unsigned int i = 0; i < limit_; ++i ) result.data_[i] = data_[i] - m.data_[i];
    std::cout << *this << " - " << m << " = " << result;
    return result;
}
Test Test::operator*(const int& scalar) const{ //Multiplication
    std::cout << std::endl<< "Multiplication operation: " <<std::endl;
    if( limit_ == 0){
        std::cout << "Multiplication warning: Empty, return Empty" <<std::endl;
        return Test();
    }
    Test result(limit_);
    for ( unsigned int i = 0; i < limit_; ++i ) result.data_[i] = data_[i] * scalar;
    std::cout << *this << " * " << scalar << " = " << result;
    return result;
}
Test operator*(const int& scalar, const Test& m){ //Multiplication
    return m * scalar;
}
std::ostream& operator<<(std::ostream& os, const Test& m){  //Show
    unsigned int limit = m.limit_;
    if (limit == 0) return os << "Empty" << std::endl;
    os << "[";
    for ( unsigned int i = 0; i < limit; ++i ){
            os << " " << std::left << std::setw(5) << m.data_[i];
    }
    os << "]" << std::endl;
    return os ;
}
void Test::reset(){ //Reset
        limit_ = 0;
        data_.reset();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-11 19:12:37

有没有办法区分时间变量和正常变量?

这就是rvalue引用的意义-- rvalue ref arg (使用&&)只能绑定到未命名的temp,而lvalue ref arg (使用&)只能绑定到命名(非临时)值。因此,您可以使用接受rvalue或lvalue推荐的版本来重载您的操作符,以避免不必要的副本。

现在,虽然您可以有多个重载运算符特殊套管所有不同的可能组合,这通常是不必要的。如果你遵循some basic guidelines,其实也不算太糟。通常,您希望将二进制运算符重载为调用赋值运算符的空闲函数(而不是方法)。如果你定义:

代码语言:javascript
复制
Test &Test::operator +=(const Test &a) {
    // add 'a' into this
    return *this; }

Test operator+(Test a, const Test &b) { return a += b; }  // free function, not a method

并有一个移动构造函数,那么通常所需的拷贝数将被最小化。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60635291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档