Default and value constructed objects have different types

17 Views Asked by At

Consider the following code with one variable default constructed and the other one value constructed:

#include <iostream>
#include <typeinfo>

struct A
{
    A() : m_() {}
    A(int m) : m_(m) {}

    int m_;
};

int main()
{
    A a, b(), c(5);

    std::cout << typeid(a).name() << std::endl;
    std::cout << typeid(b).name() << std::endl;
    std::cout << typeid(c).name() << std::endl;

    return 0;
}

With GCC 4.8 the output is

A

A ()

A

So a,c and b have different types although they refer to the same struct/class. How can this be?

(Of course, this is a minimal example derived from an issue of a complex code. There, I get something like A vs. A(int) but I can't reproduce this yet.)

1

There are 1 best solutions below

0
On

The declaration A b() means that b is a function of signature A (), notwithstanding the fact it is mixed with declarations of variables a and c of type A. The following modifications to the example code make it clearer:

struct X {};

struct A
{
    A() : m_() {}
    A(X m) : m_(m) {}

    X m_;
};

int main()
{
    X x;
    A a, b(x), c(X);

    ...
}

The output is

A

A

A (X)

So a and b are default and value constructed, resp., but c is a function declaration and thus of different type.