lundi 29 janvier 2018

#define constexpr in C++

I came across some code as shown below:

#define set_opcode(name) \
    constexpr static auto get_opcode() noexcept \
    { \
        return OpcodeValue::name; \ //OpcodeValue is some enum
    }

class NoopOpcode : public Opcode
{
public:
    set_opcode(NOOP);

    std::string serialize() const noexcept override;

    bool deserialize(const std::string &serialized) noexcept override;

    void execute(state::State &state) const noexcept override;

};


class InvOpcode : public Opcode
{

private:

    Int _item_id{};

    Int _item_count{};



public:

    set_opcode(INV);

    std::string serialize() const noexcept override;

    bool deserialize(const std::string &serialized) noexcept override;

    void execute(state::State &state) const noexcept override;

};

what is the reason/benefit for using #define to declare a constexpr?

Isn't it same thing as explicitly declaring get_opcode() {return OpcodeValue::NOOP; } inside the class NoopOpcode for example?

Aucun commentaire:

Enregistrer un commentaire