C++ bare getter setter with dispatch
A big issue with your solution is that you have to repeat yourself a lot, even when you have a macro to help you. I've come up with an alternative that doesn't use macros at all, and reduces the amount of repetition, although it still is more verbose that I would like. It uses the following class, which is similar to your class encapsulate, except that it doesn't store a pointer to a setter function, but instead has virtual getter and setter functions that can be overridden in a derived class:

template&lt;typename T&gt;
class property
{
protected:
    T v;

    virtual const T &amp;get(void) const {
        return v;
    }

    virtual void set(const T &amp;o) {
        v = o;
    }

public:
    property() {}
    property(const T &amp;v_) { set(v_); }
    property &amp;operator=(const int &amp;o) { set(o); return *this; }
    operator T() const { return get(); }
};


So now you can declare and use a property variable this way:

property&lt;int&gt; foo;
foo = 42;


When you assign a value to foo, it will call operator=(), which in turn will call the function set(). The trick is that since this is a virtual function, you can create a class or struct that inherits property&lt;int&gt;, and override the set() function. You can make use of the fact that you can declare a class or struct within another class or struct, and that you don't have to give them a name. It looks a bit weird at first, but it looks like this:

struct: property&lt;int&gt; {      // declare a nameless struct that inherits from property&lt;&gt;
    void set(const int &amp;o) { // override the set() function
        ...;
    }
} bar;                       // declare a variable named foo with this type


The main problem is that while a bare property variable works perfectly fine:

property&lt;int&gt; foo;
foo = 42;


The moment you inherit from it, the derived class has an implicitly defined assignment operator which hides the one in the base class. To get the one from the base class back, you have to add using property::operator=; to the body of the derived class. The same goes for the constructor that takes a value.

Here is your class A converted to use class property:

template&lt;typename T&gt;
class A
{
public:
    struct: property&lt;T&gt; {
        using property::operator=; // required for assigning

        void set(const T &amp;other) {
            std::cout &lt;&lt; "Setting value..." &lt;&lt; endl;
            property::set(other);
        }
    } v;

    struct: property&lt;double&gt; {
        using property::operator=; // required for assigning
        using property::property;  // required for initialization to explicit value

        void set(const T &amp;gl) {
            if (gl &lt; 0)
               property::set(0);
            else if (gl &gt; 1)
               property::set(1);
            else
               property::set(gl);
        }
    } g = 0;
};


Another option that avoids having to add the using statements is to make the derived property struct hidden, and add a base property reference to it that is public, like so:

class example {
    struct: property&lt;int&gt; {
        void set(const int &amp;o) { ... }
    } foo_;

public:
    property&lt;int&gt; &amp;foo = foo_;
};


When assigning a value to foo, it doesn't know it's actually a derived class, so it will directly call operator= from the base class, which is what we want.

Perhaps the above class property can be combined with a macro that hides the verbosity. For example:

#define PROPERTY(type, setter) \
struct: property&lt;type&gt; { \
    using property::property; \
    using property::operator=; \
    void set(const T &amp;value) { property::set(setter(value)); } \
}


This way, you can provide the name of a function that acts as the setter, or even a lambda. For example:

class B {
    static float invert(float x) {
        return -x;
    }

public:
    PROPERTY(float, invert) foo;
    PROPERTY(int, [](int v){ return v * v; }) bar = 0;
};


The drawback of this macro is that you can't easily use non-static member functions as getters and setters.