1

I understand that copy elision can occur in cases other than when returning from a function. But I'm not able to find any evidence of it. Can anyone give an example of when copy elision will occur?

I've tried the following, but none of the three copies are optimised out

class MyString
{
public:
    MyString(const char* str) : myStr(str) 
    { 
    }

    MyString(const MyString& str) : myStr(str.myStr) 
    { 
    }

    std::string myStr;
};

class MyClass
{
public:
    MyClass(MyString str) : myString(str) {} 

    MyString myString;
};



int main()
{
    MyString test("hello");
    std::cout << MyClass(test).myString.myStr << std::endl;

    return 0;
}
dan
  • 1,525
  • 1
  • 17
  • 35

0 Answers0