且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

本机 C++ 属性的可移植性

更新时间:2023-02-12 12:30:27

这与您要问的类似,并且是(我希望)标准 C++...

This is something similar to what you are asking and is (I hope) standard C++...

#include <iostream>

template<typename C, typename T, T (C::*getter)(), void (C::*setter)(const T&)>
struct Property
{
    C *instance;

    Property(C *instance)
        : instance(instance)
    {
    }

    operator T () const
    {
        return (instance->*getter)();
    }

    Property& operator=(const T& value)
    {
        (instance->*setter)(value);
        return *this;
    }

    template<typename C2, typename T2,
             T2 (C2::*getter2)(), void (C2::*setter2)(const T2&)>
    Property& operator=(const Property<C2, T2, getter2, setter2>& other)
    {
        return *this = (other.instance->*getter2)();
    }

    Property& operator=(const Property& other)
    {
        return *this = (other.instance->*getter)();
    }
};

//////////////////////////////////////////////////////////////////////////

struct Foo
{
    int x_, y_;

    void setX(const int& x) { x_ = x; std::cout << "x new value is " << x << "
"; }
    int getX() { std::cout << "reading x_
"; return x_; }

    void setY(const int& y) { y_ = y; std::cout << "y new value is " << y << "
"; }
    int getY() { std::cout << "reading y_
"; return y_; }

    Property<Foo, int, &Foo::getX, &Foo::setX> x;
    Property<Foo, int, &Foo::getY, &Foo::setY> y;

    Foo(int x0, int y0)
        : x_(x0), y_(y0), x(this), y(this)
    {
    }
};

int square(int x)
{
    return x*x;
}

int main(int argc, const char *argv[])
{
    Foo foo(10, 20);
    Foo foo2(100, 200);
    int x = foo.x; std::cout << x << "
";
    int y = foo.y; std::cout << y << "
";
    foo.x = 42; std::cout << "assigned!
";
    x = foo.x; std::cout << x << "
";
    std::cout << "same instance prop/prop assign!
";
    foo.x = foo.y;
    std::cout << "different instances prop/prop assign
";
    foo.x = foo2.x;
    std::cout << "calling a function accepting an int parameter
";
    std::cout << "square(" << foo.x << ") = " <<  square(foo.x) << "
";
    return 0;
}

正如您从 main 中看到的,只要您分配 T 类型的值(此处为 int)或隐式分配,用法是透明的可转换为 T 到属性,只要您在读取时将它们转换回 T 值.

As you can see from main the usage is transparent as long as you are assigning values of type T (here int) or implicitly convertible to T to properties and as long you are converting them back to T values on reading.

但是,如果您将 foo.x 传递给模板函数,则行为会有所不同,因为 foo.x 的类型不是 int> 但是 Property 代替.

Behavior will be different however if you for example pass foo.x to a template function because the type of foo.x is not int but Property<Foo, int, ...> instead.

您也可能遇到非模板函数的问题...调用接受 T 值的函数将工作正常,但是例如 T& 参数是一个问题,因为基本上该函数要求一个变量直接使用地址访问.出于同样的原因,您当然不能将属性的地址传递给接受 T* 参数的函数.

You can also have problems with non-template functions... calling a function accepting a T value will work fine, however a T& parameter is for example going to be a problem because basically the function is asking a variable to access directly using the address. For the same reason you cannot pass of course the address of a property to a function accepting a T* parameter.