如何在Visual Studio 2010 Professional中编译基于C ++ 11的代码 [英] How Do I Compile C++ 11 Based Code In Visual Studio 2010 Professional

查看:118
本文介绍了如何在Visual Studio 2010 Professional中编译基于C ++ 11的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨编码器,

我正在尝试用c ++ 11进行探索。但是在尝试编译以下代码时遇到很多错误,我知道错过了c ++ 11设置。

如何编译​​以下代码。

  int  main()
{
枚举 颜色
{
RED,
BLUE
} ;

enum class 水果
{
BANANA,
APPLE
} ;

颜色 a = 红色; // 注意:RED不再可访问,我们必须使用Color :: RED
Fruit b = Fruit :: BANANA ; // 注意:BANANA不再可访问,我们必须使用Fruit :: BANANA

如果 (a == b) // 这里编译错误,因为编译器不知道如何比较不同的类型颜色和水果
cout << a b 等于<< ENDL;
else
cout << a b 等于<< ENDL;

return 0;
}

解决方案

水果和颜色不是'同样的,就像在现实世界中尝试吃黄色一样。



枚举类是强烈的键入枚举 - 除非它们与结果的类型相同,否则它们不具有可比性,这与标准枚举不同,标准枚举只是整数值的语法糖。



你可以做到,但是你必须使用static_cast来做 - 而且这不是个好主意!


Visual Studio 2010仅支持一些C ++ 11功能(请参阅此处: https://msdn.microsoft .com / zh-CN / library / hh567368.aspx [ ^ ])。



但是你的错误与C ++ 11无关。您无法比较不同类型的值。您可以将值转换为相同的类型以进行比较。但这应该只在你知道自己在做什么时才能完成:

  if (static_cast< int> (a)== static_cast< int>(b))
cout<< a和b相等<< ENDL;
else
cout<< a和b不相等<< endl;


整个想法是阻止开发人员比较这两种类型的变量,而不是将它们分配给另一种。当然,如果你试图打破预期的功能,你将面临一些困难。这很好;这只是用于防止破坏代码的措施之一。谁想要你的蓝苹果?我怀疑它会食用。 :-)



但是,在极少数情况下,您可能想要对类型进行类型转换。考虑一下:

  class  Fruit {
公共
枚举值{apple,banana};
};

class ExtendedFruit {
public
< span class =code-keyword> enum 值{
apple = Fruit :: Values :: apple,banana = Fruit :: Values :: banana,
pomelo,quince,plum} ;
};



这样,您可以安全地之间进行类型转换:Fruit :: Values :: apple ExtendedFruit :: Values :: apple 。问题是:为什么不只是

  enum  Fruit {apple,banana}; 

// 下面的类型无法编译!
< span class =code-keyword> enum ExtendedFruit {apple = Fruit :: apple,banana = Fruit :: banana,pomelo};



哦,这是C ++ 11的丑陋,这是C ++极度丑陋的直接后果,需要向后兼容较旧的C ++,其中枚举类型值不能在同一名称空间中重复,因为它们曾经被使用过没有那些 TypeName :: 前缀:-(。对于C ++来说太糟糕了,太晚了。



-SA

Hi Coder,
I am trying to explore in c++11.but while trying to compile the following snap of code getting lot of error,i know missed the c++ 11 set up.
How to compile the following code.

int main()
{
    enum class Color
    {
        RED,
        BLUE
    };

    enum class Fruit
    {
        BANANA,
        APPLE
    };

    Color a = Color::RED; // note: RED is not accessible any more, we have to use Color::RED
    Fruit b = Fruit::BANANA; // note: BANANA is not accessible any more, we have to use Fruit::BANANA

    if (a == b) // compile error here, as the compiler doesn't know how to compare different types Color and Fruit
        cout << "a and b are equal" << endl;
    else
        cout << "a and b are not equal" << endl;

    return 0;
}

解决方案

A Fruit and a Color aren't the same, that like trying to eat a Yellow in the real world.

enum class is a strongly typed enum - and they are meant to not be comparable unless they are the same type as a result, unlike "standard" enums, which are just syntactic sugar for an integer value.

You can do it, but you'd have to use static_cast to do it - and it's not a good idea!


Visual Studio 2010 supports only a few C++11 features (see here: https://msdn.microsoft.com/en-us/library/hh567368.aspx[^]).

But your errors are not related to C++11. You can not compare values of different type. You may cast the values to an identical type for comparison. But this should be only done when you know what you are doing:

if (static_cast<int>(a) == static_cast<int>(b))
        cout << "a and b are equal" << endl;
    else
        cout << "a and b are not equal" << endl;


The whole idea is preventing the developer from comparing variables of these two types and from assigning one to another. Of course, if you are trying to break the intended features, you face certain difficulties. And this is good; this is just one of the measures used to prevent breaking the code. Who wants your "blue apple"? I doubt it would be edible. :-)

However, in rare cases you may want to typecast enumerations. Consider this:

class Fruit {
public:
    enum Values { apple, banana };
};

class ExtendedFruit {
public:
    enum Values {
        apple = Fruit::Values::apple, banana = Fruit::Values::banana,
        pomelo, quince, plum };
};


This way, you can safely typecast between Fruit::Values::apple and ExtendedFruit::Values::apple. The question is: why not having just

enum Fruit { apple, banana };

// The type below would not compile!
enum ExtendedFruit { apple = Fruit::apple, banana = Fruit::banana, pomelo };


Oh, this is the ugliness of C++11, which is a direct consequence of extreme ugliness of C++, the need of backward compatibility with older C++, where the enumeration type values could not be repeated in the same namespace, because they used to be used without those TypeName:: prefixes :-(. Too bad and too late for C++.

—SA


这篇关于如何在Visual Studio 2010 Professional中编译基于C ++ 11的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆