c++ - Qt 5.9 中的 Q_FLAG_NS 不提供按位运算符

标签 c++ qt5

应该如何使用Q_FLAG_NS宏?

我对Q_FLAG_NS的阅读和 KDAB's New Qt Support Namespaces建议我使用这个宏应该提供按位运算符,但无论我尝试什么,

../main.cpp:11:11: error: invalid operands to binary expression 
('App::ComponentRequirementState' and 'App::ComponentRequirementState')
    r = r | App::ComponentRequirementState::AlwaysRequired;
        ~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我放置了一个最小代码示例 here .

奇怪的是,我可以在 ComponentRequirementStat 枚举的定义内使用按位运算符,但不能在其外部使用。

我使用这个宏是错误的吗?或者它根本不起作用?

当我手动定义运算符时,例如,

auto operator|(const App::ComponentRequirementState a, App::ComponentRequirementState b) -> App::ComponentRequirementState
{
    return static_cast<App::ComponentRequirementState>(static_cast<unsigned int>(a) | static_cast<unsigned int>(b));
}

然后就可以了。

最佳答案

使用 Qt

Q_FLAGS_NS不提供按位运算支持:它只是在 Qt metaobject system 中注册类型。 .

如果您想注册类型并提供类型安全的按位运算符支持,您应该使用 QFlags .

文档中的一个示例是:

class MyClass
{
public:
    enum Option {
        NoOptions = 0x0,
        ShowTabs = 0x1,
        ShowAll = 0x2,
        SqueezeBlank = 0x4
    };
    Q_DECLARE_FLAGS(Options, Option)
    ...
};

Q_DECLARE_OPERATORS_FOR_FLAGS(MyClass::Options)

您也可以跳过大部分内容并执行以下操作:

// not type-safe, unscoped enum. You should likely define this
// in a namespace or class.  
enum Option {
    NoOptions = 0x0,
    ShowTabs = 0x1,
    ShowAll = 0x2,
    SqueezeBlank = 0x4
};

// type-safe flag
using Options = QFlags<Option>;

您还可以使用无作用域枚举来实现此确切目的,或者使用宏来帮助您为有作用域枚举提供按位运算符支持:

使用作用域枚举进行类型安全的位运算

这是一个您可以自由使用的宏(公共(public)领域,我自己的代码,尽管您可能应该修改该宏以确保它具有前缀以避免任何名称冲突)以将按位运算添加到作用域枚举。目前,这需要 C++14 支持,但是,更改 std::underlying_type_t<T>typename std::underlying_type<T>::type允许宏在 C++11 中工作。

使用

enum class enum1_t
{
    A = 1,
    B,
    C,
    D,
    E,
};


enum class enum2_t
{
    F = 1,
    G,
    H,
    I,
    J,
};

ENUM_FLAG(enum1_t)            // allow bitwise operations for enum1_t and enum1_t
ENUM_FLAG(enum1_t, enum2_t)   // allow bitwise operations for enum1_t and enum2_t

代码

#include <type_traits>
#include <cstdint>

// HELPERS
// -------

/**
 *  \brief Get enum underlying type.
 */
template <typename T>
inline std::underlying_type_t<T> int_t(T t)
{
    return static_cast<std::underlying_type_t<T>>(t);
}

// MACROS
// ------

/**
 *  \brief Macro to define enum operators between enumerations.
 *
 *  Supports `&`, `&=`, `|`, `|=`, `^`, `^=`, `~`, and bool conversion.
 */
#define ENUM_FLAG2(lhs_t, ths_t)                                        \
    /*  \brief Bitwise or operator. */                                  \
    inline lhs_t operator|(lhs_t lhs, ths_t rhs) noexcept               \
    {                                                                   \
        return static_cast<lhs_t>(int_t(lhs) | int_t(rhs));             \
    }                                                                   \
                                                                        \
    /*  \brief Bitwise or assignment operator. */                       \
    inline lhs_t & operator|=(lhs_t &lhs, ths_t rhs) noexcept           \
    {                                                                   \
        lhs = static_cast<lhs_t>(int_t(lhs) | int_t(rhs));              \
        return lhs;                                                     \
    }                                                                   \
                                                                        \
    /*  \brief Bitwise and operator. */                                 \
    inline lhs_t operator&(lhs_t lhs, ths_t rhs) noexcept               \
    {                                                                   \
        return static_cast<lhs_t>(int_t(lhs) & int_t(rhs));             \
    }                                                                   \
                                                                        \
    /*  \brief Bitwise and assignment operator. */                      \
    inline lhs_t & operator&=(lhs_t &lhs, ths_t rhs) noexcept           \
    {                                                                   \
        lhs = static_cast<lhs_t>(int_t(lhs) & int_t(rhs));              \
        return lhs;                                                     \
    }                                                                   \
                                                                        \
    /*  \brief Bitwise xor operator. */                                 \
    inline lhs_t operator^(lhs_t lhs, ths_t rhs) noexcept               \
    {                                                                   \
        return static_cast<lhs_t>(int_t(lhs) ^ int_t(rhs));             \
    }                                                                   \
                                                                        \
    /*  \brief Bitwise xor assignment operator. */                      \
    inline lhs_t & operator^=(lhs_t &lhs, ths_t rhs) noexcept           \
    {                                                                   \
        lhs = static_cast<lhs_t>(int_t(lhs) ^ int_t(rhs));              \
        return lhs;                                                     \
    }


/**
 *  \brief Set enumeration flags within the same enum.
 */
#define ENUM_FLAG1(enum_t)                                              \
    ENUM_FLAG2(enum_t, enum_t)                                          \
                                                                        \
    /*  \brief Bitwise negation operator. */                            \
    inline enum_t operator~(enum_t value) noexcept                      \
    {                                                                   \
        return static_cast<enum_t>(~int_t(value));                      \
    }                                                                   \
                                                                        \
    /*  \brief Negation operator. */                                    \
    inline bool operator!(enum_t value) noexcept                        \
    {                                                                   \
        return int_t(value) == 0;                                       \
    }

/**
 *  \brief Macros to grab the proper bit-wise flag setter.
 *  `ENUM_ID` is required for MSVC compatibility, since MSVC
 *  has issues in expanding `__VA_ARGS__` for the dispatcher.
 *  Don't remove it, even if the above code works without it 
 *  for GCC and Clang.
 */
#define ENUM_ID(x) x
#define GET_ENUM_FLAG(_1,_2,NAME,...) NAME
#define ENUM_FLAG(...) ENUM_ID(GET_ENUM_FLAG(__VA_ARGS__, ENUM_FLAG2, ENUM_FLAG1)(__VA_ARGS__))

关于c++ - Qt 5.9 中的 Q_FLAG_NS 不提供按位运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48269664/

相关文章:

c++ - macOS 上的内核与用户空间音频设备驱动程序

c++ - 匹配别名模板作为模板参数

c++ - Windows 7 和 C++ : Cross compiling application for use on Raspberry Pi

Qt - 如何使用 QDir::entryList() 同时过滤具有特定文件类型和目录的文件

c++ - 除非将操作添加到工具栏,否则 Qt5 无法识别快捷方式

c++ - 在C++中为初始化二维数组赋值

c++ - VS 2012 中的显式模板声明/定义

android - 通过pyqtdeploy和Qt5将PyQt5应用部署到Android

c++ - QT MySql 数据库检查登录名和密码以登录

c++ - QQuickPaintedItem 使用 QPainter 缓慢更新