kyb kyb - 1 year ago 91
C++ Question

Automaticly add CV-qualified functions

I have next class

class Parameter
T value_;

// Get
T& value() { return value_; }
volatile T& value() volatile { return value_; }
const T& value() const { return value_; }
const volatile T& value() volatile const { return value_; }

How can I minimize number of lines, amount of code. I want to write once
T& value()
and achieve CV-qualified versions automatically. Possible?

Answer Source

Yes, you could use macro, but I can't see any use of such:

#define IDK_WHY(returnType, functionName, functionArgs, functionBody) \
                   returnType functionName functionArgs                functionBody \
    volatile       returnType functionName functionArgs volatile       functionBody \
    const          returnType functionName functionArgs const          functionBody \
    const volatile returnType functionName functionArgs volatile const functionBody

class Parameter
    using T = int; //for testing, dunno what is T in your code
    T value_;

    // note you can't use comma in last argument, it would require more complex macro
    IDK_WHY(T&, value, (), { return value_;}) //done
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download