1

My code has a more complex version of basically this situation:

template <typename T>
struct A {
    T t;
    int i;

    A(T t)
        : t(t)
    {
    }
};

struct S {
};

int main()
{
    A<S> a(S());
    std::cout << a.i << std::endl; // this line does not compile
    return 0;
}

I am running into an error which is on every compiler I tried (Clang, GCC and MSVC). Specifically, GCC says:

request for member ‘i’ in ‘a’, which is of non-class type ‘A(S (*)())’

Why is this S (*)() even mentioned? Looks like despite I declared S as a struct a bunch of lines above, it sees it as a function pointer or something. Why are all the compilers confused by this? How can A<S> suddenly be a non-class type because a misinterpreted constructor call? And why is the error not on the constructor call but on the access of .i?

P.S. - The fix can be achieved in many different ways:

A<S> a(S{});

A<S> a{S()};

A<S> a{S{}};

auto a = A<S>(S());

So please keep in mind the question is not about the fix, but the reason for the compiler confusion.

nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64

0 Answers0