boost::addable2的使用-为什么从operator+返回的结果是相反的

usage of boost::addable2 - why the returned result from operator+ is reversed

本文关键字:返回 operator+ 结果是 addable2 boost 为什么      更新时间:2023-10-16
#include <iostream>
#include <string>
#include "boost/operators.hpp"
using namespace std;
class simple_string : private boost::addable1<simple_string,
                              boost::addable2<simple_string, const char*> > 
{
public:
  simple_string() : m_str("Default")
  {}
  explicit simple_string(const char* s) : m_str(s)
  {}
  simple_string(const simple_string& rhs) : m_str(rhs.m_str)
  {}
  simple_string operator+=(const simple_string& rhs)
  {
    m_str += rhs.m_str;
    return *this;
  }
  simple_string operator+=(const char* rhs)
  {
    this->operator+=(simple_string(rhs));
    return *this;
  }
  friend ostream& operator<<(ostream& os, const simple_string& si)
  {
    os << si.m_str;
    return os;
  }
private:
  string m_str;
};
int main(void)
{
  simple_string s1;
  simple_string s2(s1);
  cout << "[before] s2: " << s2 << endl;
  s2 += s1;
  cout << "s2 += s1: " << s2 << endl;
  simple_string s3 = s1 + s2;
  cout << "s3: " << s3 << endl;
  simple_string s4 = s3 + "Why";
  simple_string s5 = "Now" + s3;
  cout << "s4: " << s4 << endl;
  cout << "s5: " << s5 << endl;
  cout << "Now" + simple_string() << endl;
  return 0;
}

~/Documents/C++/boost $ g++ -o p123 p123.cpp
~/Documents/C++/boost $ ./p123
[before] s2: Default
s2 += s1: DefaultDefault
s3: DefaultDefaultDefault
s4: DefaultDefaultDefaultWhy
s5: DefaultDefaultDefaultNow
DefaultNow
~/Documents/C++/boost $ 

问题>为什么生成的operator+(const char*, const simple_string&)没有返回正确的结果?

例如,s5的预期结果应该是s5: NowDefaultDefaultDefault

"Now" + simple_string()的结果应为NowDefault

问题是,对于字符串operator+是不可交换的,但boost实现假设它是。详情请参阅http://www.boost.org/doc/libs/1_32_0/libs/utility/operators.htm#symmetry的对称说明。

编辑:在阅读实际的boost代码,我认为我错了。您不能对operator+不可交换的类使用boost操作符生成,因为这是boost中的隐式假设。你必须自己实现它。

基于(我相信)错误阅读文档的原始答案:

您可以强制执行不假设对称的可能效率较低的代码,如下所示:

...if your code relies on the function signature or a strict symmetric behaviour, you should set BOOST_FORCE_SYMMETRIC_OPERATORS in your user-config. This will force the NRVO-friendly implementation to be used even for compilers that don't implement the NRVO.