源代码可以在 GCC 中编译,但不能在 Visual Studio 中编译
这可以在 GCC 4.6 上编译,但不能在 VS2010 sp1 上编译:
是我的错还是VS又搞砸了?
#include "stdafx.h"
enum Attribute{red_att,black_att,key_att,value_att};
struct Color{};
template<enum Attribute>
struct Tag_Value;
template<>
struct Tag_Value<red_att>
{
typedef Color type;
};
int main()
{
return 0;
}
错误:错误C2599:'Attribute':不允许枚举类型的前向声明
错误C2440:'specialization':无法从'Attribute'转换为'Attribute'
This compiles on GCC 4.6 but doesn't with VS2010 sp1:
Is it my fault or VS screwed up again?
#include "stdafx.h"
enum Attribute{red_att,black_att,key_att,value_att};
struct Color{};
template<enum Attribute>
struct Tag_Value;
template<>
struct Tag_Value<red_att>
{
typedef Color type;
};
int main()
{
return 0;
}
Errors:error C2599: 'Attribute' : forward declaration of enum type is not allowed
error C2440: 'specialization' : cannot convert from 'Attribute' to 'Attribute'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(3)
假设一个有效且不冲突的
stdafx.h
,看起来像是有效的代码。您会发现人们告诉您,在 C++ 中,如果没有任何内容隐藏
Name
(例如一个名为Name
的函数)。在 C 中你必须这样做,因为 C 对于查找名字有不同的概念。但在 C++ 中,要引用结构、类、联合或枚举,只需使用Name
即可。因此,您可以使用Attribute
而不是enum Attribute
。但是命名类型的不同选择不应使编译器拒绝您的代码。Assuming a valid and non-conflicting
stdafx.h
, that looks like valid code.You will find people tell you that in C++, you don't have to say
enum Name
orstruct Name
if nothing is hidingName
(like a function calledName
). In C you have to, because C has a different concept for looking up names. But in C++, to refer to a struct, class, union or enum, you can just useName
. So you can useAttribute
instead ofenum Attribute
. But the different choice of naming the type should not make the compiler reject your code.我不确定确切的问题是什么 - 我的猜测是它将枚举属性视为新枚举声明的开始......也许作为 Tag_Value 的成员。因此,您有两个 Attribute 枚举,并且它不会让您将其中一个与另一个进行专门化。
要解决此问题,只需删除
enum
:即可:
I'm not certain what the exact problem is—my guess is that it is treating
enum Attribute
as the beginning of a new enum declaration... perhaps as a member of Tag_Value. So you have twoAttribute
enums and it won't let you specialize one with the other.To fix, just get rid of the
enum
:to this:
您必须更改此行:
为此:
假设您想要接受
Attribute
类型的模板参数(枚举是一种类型,是的)。You'll have to change this line:
To this:
Assuming you'll want to accept a template argument of type
Attribute
(an enum is a type, yes).