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.
a(S());` declares a function called `a` that has the return type`A– François Andrieux Apr 02 '19 at 11:28` and it's argument is a function pointer to a function with no argument that returns an `S` (`S(*)()`).