模板c ++的模板? [英] template of template c++?

查看:72
本文介绍了模板c ++的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法用我们期望的所有东西来创造一些美酒类。我的意思是,使用它时,您无需仅使用 operator = 即可调用函数,即可完成所有工作。但是我想只有一件事,如果我们可以解决,那就更好了:

i've managed to create some preperty class with all the thing we expect from one. I mean when using it you don't need to call functions just using operator = will do all the work. but there is only one thing I guess it would be nice if we could resolve :

template <class T, class X,void (T::*setFunc)(const X&),const X& (T::*getFunc)()const> class property
{ 
    T* const owner;
    X data;
    friend T;
    property(T*const  pOwner) : owner (pOwner)
    {
    }
public:
    property& operator = (const X& input){(owner->*setFunc)(input);return *this;}
    operator const X&()const {return (owner->*getFunc)();}
};

struct c
{
protected:
    void setInt(const int& data);
    const int& getInt() const;
public:
    c();
    property<c, int ,&setInt,&getInt> myInt;
};

c::c() : myInt(this)
{
}

void c::setInt(const int& data)
{
    myInt.data = data;
}
const int& c::getInt() const
{
    return myInt.data;
}

请参阅class属性有4个参数,第一个参数是类类型本身。我想知道我们是否可以做任何事情来从两个函数指针属性的需要中提取类类型。诸如 property< int,& setInt,& getInt> myInt;

see class property has 4 arguments and the first argument is the class type itself. I'd like to know if we could possibly do anything to extract class type from two function pointers property needs. somwthing like property <int, &setInt, &getInt> myInt;.

您知道消除第一个模板参数的任何方法吗?

do you know any way to eliminate first template parameter?

推荐答案

如果您想省略显式指定类型参数,则以下代码将满足
的目的。
但是,此代码需要VC2010。

If you'd like to omit specifying the type parameters explicitly, the following code will meet the purpose. However, this code requires VC2010.

template <class> struct class_type;
template <class C, class T> struct class_type< T(C::*) > { typedef C type; };

template <class> struct param_type;
template <class C, class T> struct param_type< void(C::*)(const T&) > {
    typedef T type;
};

template <class S, S setFunc, class G, G getFunc> struct property {
    typedef typename class_type<S>::type T;
    typedef typename param_type<S>::type X;
    T* const owner;
    X data;
    ....
};

#define PROPERTY(set, get) property<decltype(&set), &set, decltype(&get), &get>

struct c {
    void setInt(const int& data);
    const int& getInt() const;
    PROPERTY(setInt, getInt) myInt;
};

顺便说一句,MSVC有自己的
属性
如果达到目的,这可能会更容易。

Incidentally, MSVC has its own property. Probably this is easier if it serves the purpose.

这篇关于模板c ++的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆