0

I'm reading about copy-constructors and the differences between copy initialization and direct initialization. I know that copy and direct initialization differ only for (user-defined) class types and that the first implies sometimes an implicit conversion (by a converting constructor).For example if I could initialize an object of class A:

class A {
  public:
    // default constructor
    A() : a(1) {}
    // converting constructor
    A(int i) : a(i) {}
    // copy constructor
    A(const A &other) : a(other.a) {}
  private:
    int a;
};

int main()
{
  // calls A::A(int)
  A a(10);
  // calls A::A(int) to construct a temporary A (if converting constructor is not explicit) 
  // and then calls A::A(const A&) (if no elision is performed)
  A b=10;
  // calls copy constructor A::A(const A&) to construct an A from a temporary A
  A c=A();
  // what does this line do?
  A d(A());
} 

I read that the last line does not create an A by direct initializing d from a temporary A() but it declares a function named d,which returns an A and takes an argument of function type ( a pointer to a function really ). So my question is, is it true? and if it's true, how can I perform direct initialization of an object from a temporary like:

type_1 name(type_2(args));

assuming that type_1 has a contructor that takes a parameter of type type_2

is it something that can not be done?

Luca
  • 1,658
  • 4
  • 20
  • 41
  • Declares a function called `d` returning `A`, and taking as argument: pointer to function taking no arguments and returning `A`. You could do `A d{A()};` which declares an object `d` using direct-list-initialization., or `A d((A()));` using direct-initialization. – M.M Sep 08 '15 at 23:12
  • With my compiler, `A b=10;` only calls `A::A(int)`. Is this the standard, or is it due to compiler elision? If it's standard, then wouldn't `type_1 name = type_2(args);` also perform direct initialization from a temporary? It does with my compiler. – Darryl Sep 08 '15 at 23:31
  • @Darryl Yes,sometimes it does perform elision,but I don't know precisely when or why it happens,or if it's implementation dependent.But my question was about syntax. – Luca Sep 09 '15 at 06:53

0 Answers0