Forward-declared type and "non-class type that has already been declared as a class type"

I have a problem with the following code:

template <typename T> void foo(struct bar & b); struct bar {}; int main(){} 

It compiles successfully on GCC, but crashes in MSVC (2008) with the following error:

C2990: 'bar' : non-class type as already been declared as a class type

Is the code incorrect or is it an error in MSVC?

It works if I add a struct bar; before defining the template.

+6
source share
5 answers

And we have a 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 inappropriate behavior in VC ++. However, a simple workaround is to reorder the declarations so that the String-to-String declaration is known when the template declaration is encountered. Due to the low severity of this error and our priorities, we regret that we cannot fix the error in the next release of the compiler, but we will consider it for a future release.

Hi,

Ghani's Tunnel Visual C ++ Team

+3
source

In most situations, the C (or C ++) compiler runs strictly from top to bottom in the source code. Therefore, you need a forward declaration before attempting to reference the struct bar , otherwise the compiler will not know that it exists.

+3
source

You most likely have a struct bar {}; somewhere above this block of code (possibly in the header file). See http://msdn.microsoft.com/en-us/library/zfcw8kk9.aspx

Edit: Also from the link above:

C2990 may also occur due to a hacking change in the Visual C ++ Compiler for Visual C ++ 2005; the compiler now requires that multiple declarations of the same type be identical with respect to the template specification.

Since foo is a template and bar "declared forward" in the argument list of foo , what happens if you move struct bar {}; higher foo ?

0
source

This is like valid code. No matter what the MSVC does, this seems to be some kind of strange behavior mismatch from what I see.

0
source

Source: https://habr.com/ru/post/888057/


All Articles