标签: enum-class

C++ 11中的静态导入(例如枚举类)

我对枚举类(VS2012)的使用:

class matrix {
public:
    enum class operation_type {ADD, MULT};
    matrix(operation_type op);
...
}
Run Code Online (Sandbox Code Playgroud)

在我使用的另一个片段中

matrix* m = new matrix(matrix::operation_type::ADD);
Run Code Online (Sandbox Code Playgroud)

如果名字很长,这就变得非常混乱.

有可能以某种方式导入枚举值,以便我可以写:

matrix* m = new matrix(ADD);
Run Code Online (Sandbox Code Playgroud)

嵌套类也一样 - 我可以导入它们吗?

c++ enums c++11 enum-class

5
推荐指数
1
解决办法
1126
查看次数

QSettings中QVariant中的枚举类

我有枚举类,QVariants和QSettings类的问题.我想要存储在QVariant中的枚举类值,该QVariant进入QSettings实例.所以,我的代码实际上看起来像这样:

enum class Foo
{
    Bar1, Bar2
}
Q_ENUMS(Foo)
Q_DECLARE_METATYPE(Foo)

...

Foo value = Bar2;
QSettings settings;
settings.setValue(QString("Foo"), QVariant::fromValue(value));
Run Code Online (Sandbox Code Playgroud)

此时执行代码时,断言跳进并抱怨:

ASSERT failure in QVariant::save: "Invalid type to save", file kernel\qvariant.cpp
Run Code Online (Sandbox Code Playgroud)

在互联网上搜索,我发现该类缺少一个合适的<<和>>运算符.但这不是枚举类的选项.我甚至试过用

qRegisterMetaType<Foo>("Foo");
Run Code Online (Sandbox Code Playgroud)

但它没有帮助.也许你有一些其他的建议/解决方案.谢谢!

qvariant qsettings c++11 enum-class qt5

5
推荐指数
1
解决办法
2737
查看次数

使用带有Boost测试的枚举类

我有一个我想在单元测试中使用的枚举类:

enum class MyEnumClass
{
    MyEntryA,
    MyEntryB
};
Run Code Online (Sandbox Code Playgroud)

我想用它如下:

MyEnumClass myEnumValue = MyEnumClass::MyEntryA;
BOOST_CHECK_EQUAL(myEnumValue, MyEnumClass::MyEntryB);
Run Code Online (Sandbox Code Playgroud)

但是我得到了这个错误,显然因为boost测试试图输出值:

include/boost/test/test_tools.hpp:326:14: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
         ostr << t; // by default print the value
              ^
Run Code Online (Sandbox Code Playgroud)

添加丑陋的static_cast"解决"问题:

BOOST_CHECK_EQUAL(static_cast<int>(myEnumValue), static_cast<int>(MyEnumClass::MyEntryB));
Run Code Online (Sandbox Code Playgroud)

但是我想避免为每个枚举类做到这一点.我还想避免<<为每个枚举类定义流操作符.

是否有更简单的方法来使用带有boost测试的枚举类?

或者其他单元测试框架是否有更好的方法来处理枚举类?

c++ boost-test enum-class

5
推荐指数
2
解决办法
1230
查看次数

对于具有受限存储的枚举,类型转换在switch中失败

SSCCE:

enum class confirm {yes};

struct item
{
  confirm s:4; // (1) limiting storage size required
};

int main()
{
  item itm;

  itm.s = confirm::yes; // (2) OK

  switch (itm.s)
  {
    case confirm::yes: // (3) Failure, need static data cast here?
      break;
  }
}
Run Code Online (Sandbox Code Playgroud)

产生错误:

In function ‘int main()’:
error: could not convert ‘yes’ from ‘confirm’ to ‘int’
 case confirm::yes:
               ^
Run Code Online (Sandbox Code Playgroud)

g ++编译但用clang ++编译好.为什么分配标记为(2)可能但是由(3)标记的案例条款不是?

警告约too small storageofftopic

c++ g++ switch-statement bit-fields enum-class

5
推荐指数
1
解决办法
120
查看次数

类vs枚举类作为索引类型

P0138R2建议1开始

在现代C++ 11程序中引入一种几乎完全复制但又不同类型的新整数类型有一种非常有用的技术: enum class具有明确指定的底层类型.例:

enum class Index : int { };    // Note: no enumerator.
Run Code Online (Sandbox Code Playgroud)

可以使用Index一个新的不同整数类型,它没有隐式转换为任何东西(好!).

要转换Index为其基础类型,定义是很有用的

int operator*(Index index) {
    return static_cast<int>(index);
}
Run Code Online (Sandbox Code Playgroud)

另一种创建Index类型的方法是使用old class:

class Index final {
public:
     explicit Index(int index = 0) : index_(index) { }

     int operator*() const {
         return index_;
     }

private:  
     int index_;
};
Run Code Online (Sandbox Code Playgroud)

两者似乎都大致相同,可以以相同的方式使用:

void bar(Index index) {
    std::cout << *index;
}

bar(Index{1});

int i = 1;
bar(Index{i});
Run Code Online (Sandbox Code Playgroud)

Pro enum …

c++ c++11 enum-class

5
推荐指数
1
解决办法
187
查看次数

自我记录代码是否值得潜在的性能问题?

我创建了一个小类,允许我使用强类型枚举的枚举器作为标志(组合).我正在使用type_traits进行底层类型检测,因此它应该是稍微类型安全的,并且主要在编译时处理.但是,我想知道它是否真的值得.

我现在可以写一些像

void Foo(Flags<Mode> Value);
Run Code Online (Sandbox Code Playgroud)

并且程序员将看到他只能使用Mode中的枚举器(例如Mode :: Read),并且他也不能将任何其他枚举与Mode结合使用.你觉得它比...更好吗?

void Foo(int Mode);
Run Code Online (Sandbox Code Playgroud)

,我不确定人们是否能欣赏它?

c++ enums templates self-documenting-code enum-class

4
推荐指数
1
解决办法
244
查看次数

如何从boost :: property_tree获取枚举?

如何从中获取枚举boost::property_tree

这是我的"不工作"的例子.

config.xml中

<root>
  <fooEnum>EMISSION::EMIT1</fooEnum>
  <fooDouble>42</fooDouble>
</root>
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

int main()
{
  enum class EMISSION { EMIT1, EMIT2 } ;
  enum EMISSION myEmission;

  //Initialize the XML file into property_tree
  boost::property_tree::ptree pt;
  read_xml("config.xml", pt);

  //test enum (SUCCESS)
  myEmission = EMISSION::EMIT1;
  std::cout << (myEmission == EMISSION::EMIT1) << "\n";

  //test basic ptree interpreting capability (SUCCESS)
  const double fooDouble = pt.get<double>("root.fooDouble");
  std::cout << fooDouble << "\n";

  //read from enum from ptree and assign (FAILURE)
  myEmission = pt.get<enum …
Run Code Online (Sandbox Code Playgroud)

c++ boost-propertytree xml-parsing c++11 enum-class

4
推荐指数
1
解决办法
1590
查看次数

在switch语句中从int到enum类的隐式转换

enum class pid
{
    Alpha, Beta, Gamma
};

int main()
{
    int propId = 2;
    switch(propId)
    {
    case pid::Alpha:
    case pid::Beta:
    case pid::Gamma:
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的片段在msvc2012中编译好(并且有效),但在clang-3.4和g ++ - 4.8中失败.这些需要static_cast<pid>(propId)在switch子句中使用.

顺便说一句,没有显式转换的简单赋值,例如pid a = propId;在每个编译器中给出错误.

哪一个做对了?

c++ type-conversion c++11 enum-class

4
推荐指数
1
解决办法
7785
查看次数

在 C++ 中调用带括号的枚举/枚举类时会发生什么?

这可能是一个有点奇怪的问题,但我真的不知道如何更好地表达它。

我刚刚发现我可以执行以下操作:

#include <iostream>

enum class Colour  // also works with plain old enum
{
    Red = 1,
    Green,
    Blue,
    Yellow,
    Black,
    White
};

int main()
{
    Colour c = Colour(15);  // this is the line I don't quite understand

    std::cout << static_cast<int>(c) << std::endl;  // this returns 15

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以现在我在 类型的变量中有整数值 15 Colour

这里到底发生了什么?这是某种枚举“构造函数”在起作用吗?据我所知,整数值 15 没有放入枚举中,它仅存储在变量 中c。首先,为什么这样的东西会有用——创建一个枚举中不存在的值?

c++ enums c++11 enum-class

3
推荐指数
1
解决办法
1288
查看次数

在可能的类中使用C ++ 20中的“使用枚举”吗?

此答案中,提到了在即将到来的C ++ 20标准中可以使用usingon语句,enum class并将枚举字段导入本地名称空间。

我想知道这是否还意味着我也可以在这样的类定义中使用它:

class Foo {
    enum class Color
    {
        red, 
        blue
    };
    using enum Color;
};

int main()
{
    Foo::Color c = Foo::red;
}
Run Code Online (Sandbox Code Playgroud)

还是我仍然需要提供完整的名称空间?:

    Foo::Color c = Foo::Color::red;
Run Code Online (Sandbox Code Playgroud)

我在wandbox.org中尝试过,但似乎gcc和clang都不知道using enum

c++ enums enum-class c++20

3
推荐指数
1
解决办法
130
查看次数