Andrew Loomis
Andrew Loomis

Reputation: 135

Constexpr in class member parameters

I'd like to specify a variable in the template parameters of std::array in various parts of my class declaration like so:

class SetAngles
    {
    public:
        constexpr int txSize() const { return 19; }
        constexpr int rxSize() const { return ack.size(); }

        void txParse(std::array<uint8_t, txSize()>& packet)
        {
            ...
        }

    private:
        std::array<uint8_t, txSize()> txPacket = {0xFA, 0x0E, 0x11, 0x00, 0x00, 0x00, 0x00, 
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
};

So I don't have to change a constant a whole bunch of times in various places if I need to change it. Apparently what's above isn't how constexpr works... I have also tried using a public member variable instead of a function, still fails. I get the compiler error:

cannot call member function ‘constexpr int GimbalPacket::SetAngles::txSize() const’ without object

I know a #define statement could work, but I was hoping to keep this encapsulated within the class. Any suggestions on how to do this?

Upvotes: 1

Views: 388

Answers (1)

Andrew Loomis
Andrew Loomis

Reputation: 135

I wasn't able to use constexpr within the class as I initially wanted to. Ended up just putting a constexpr outside the class and wrapping both in a namespace to accomplish the encapsulation.

Upvotes: 1

Related Questions