开发者

Source compiles in GCC but not Visual Studio

开发者 https://www.devze.com 2023-03-22 05:36 出处:网络
This compiles on GCC 4.6 but doesn\'t with VS2010 sp1: Is it my fault or VS screwed up again? #include \"stdafx.h\"

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: 'speci开发者_如何转开发alization' : cannot convert from 'Attribute' to '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 or struct Name if nothing is hiding Name (like a function called Name). 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 use Name. So you can use Attribute instead of enum Attribute. But the different choice of naming the type should not make the compiler reject your code.


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 two Attribute enums and it won't let you specialize one with the other.

To fix, just get rid of the enum:

template<enum Attribute> struct Tag_Value;

to this:

template<Attribute> struct Tag_Value;


You'll have to change this line:

template<enum Attribute> struct Tag_Value; 

To this:

template<Attribute> struct Tag_Value;

Assuming you'll want to accept a template argument of type Attribute (an enum is a type, yes).

0

精彩评论

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