I have problem with following code:
template <typename T>
void foo(struct bar & b);
struct bar {};
int main(){}
It compiles successfuly on GCC, but fails on MSVC (2008) with following error:
C2990: 'bar' : non-class type as already been declared as a class type
Is the code wrong or it's a bug in MSVC?
It works if I add struct bar;
before template definition.
And we have our winner:
https://connect.microsoft.com/VisualStudio/feedback/details/668430/forward-declared-type-and-non-class-type-as-already-been-declared-as-a-class-type
Thank you for reporting this issue. This is indeed a case of non-conformant behaviour in VC++. However, a simple workaround is to reorder the declarations so that the declaration "struct bar" is known when the template declaration is encountered. Due to the low severity of this bug and our priorities, we regret that we cannot fix the bug in the next release of the compiler but we will consider it for a future release.
Regards,
Tanveer Gani Visual C++ Team
In most situations, a C (or C++ compiler) works strictly top-to-bottom on your source code. So you need a forward declaration before you ever attempt to reference struct bar
, otherwise the compiler will not know that it exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With