1

I have the following class definition:

class T
{
    string text;

public:
    T() : text{} 
    {
        // ...
    }

    T(T &other)
    {
        // ...
    }
};

And the following initialization:

T cp_f = T();

From what I can understand about object initialization this is a value (default) initialization (T()) followed by a copy initialization (T cp_f = ...). But when I execute this code the copy constructor is never called. At first I assumed that the compiler is optimizing the redundant temporary but even with optimization turned off the behavior persists.

Why this expression does not result in a copy initialization?

PS: I'm using the VC++ compiler version 19.10.25017 for x86/

Matan Shahar
  • 3,190
  • 2
  • 20
  • 45
  • @MarkRansom This is invalid C++14 because of the missing `const` on the copy constructor (which still has to exist and be callable even though the call is elided), and valid C++17. MSVC doesn't claim to implement the C++17 rules, so I think this is their extension allowing non-const lvalue refs to bind to rvalues at work again. – T.C. Mar 24 '17 at 20:41
  • @T.C. good point, I missed that detail. But I still think the duplicate question provides an answer. – Mark Ransom Mar 24 '17 at 20:53

0 Answers0