开发者

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

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:

C299开发者_如何学JAVA0: '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.


Anyway, I've posted a bug in Microsoft Connect: https://connect.microsoft.com/VisualStudio/feedback/details/668430/forward-declared-type-and-non-class-type-as-already-been-declared-as-a-class-type


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

Edit: Also from the link above:

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

Since foo is templated and bar is being "forward-declared" in the foo argument list, what happens if you move struct bar {}; above foo?


That looks like valid code. Whatever MSVC is doing, it appears to be some weird non-conforming behavior, from what I can see.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜