jeudi 18 avril 2019

Is `A a = A()` valid? What operators / methods are invoked underneath?

Given the following code:

#include <iostream>

class A {
 public:
  A() { std::cout << "ctor" << std::endl; }
  A(const A& o) { std::cout << "copy ctor" << std::endl; }
  A& operator=(const A& o) { std::cout << "copy asgnmt" << std::endl; }
};

int main() {
  A a = A();
  return 0;
}

The above code gets complied with g++ 4.8.4 on Ubuntu 14.04:

g++ -g -o test test.cpp

And output:

ctor

Is A a = A(); compliant with C++ standard(s)? Or is that just UB therefore compiler dependent? If that code is standard compliant then which methods are invoked underneath? A() should return nothing at all, shouldn't it?

Aucun commentaire:

Enregistrer un commentaire