我已经成功地创建了一些preperty类,其中包含了我们期望从一个类中获得的所有功能。我的意思是,当使用它时,你不需要调用函数,只需使用operator =就可以完成所有的工作。但只有一件事我想如果我们能解决就好了:
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;这样的东西。
你知道消除第一个模板参数的方法吗?
发布于 2011-07-04 22:17:13
如果您想要省略显式地指定类型参数,下面的代码可以满足您的需要。但是,此代码需要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有自己的property。如果它服务于目的,这可能会更容易。
发布于 2011-07-04 23:00:57
终于成功了!http://ideone.com/XJ7of
这个稍好一点的版本只适用于Comeau (不确定Comeau或gcc是正确的,但gcc抱怨friend的名称)。
#include <iostream>
#include <typeinfo>
template <class T, class X,void (T::type::*setFunc)(const typename X::type&),const typename X::type& (T::type::*getFunc)()const> class property_impl
{
typename T::type* const owner;
friend typename T::type;
property_impl(typename T::type* const pOwner) : owner (pOwner)
{
}
public:
property_impl& operator = (const typename X::type& input){(owner->*setFunc)(input); return *this;}
operator const typename X::type&()const {return (owner->*getFunc)();}
};
template<typename T> struct identity { typedef T type; };
template<typename Arg, typename T>
identity<T> match_memfn_classtype( void (T::*fn)(Arg) );
template<typename Arg, typename T>
identity<Arg> match_memfn_argtype( void (T::*fn)(Arg) );
#define property(setter,getter) property_impl<decltype(match_memfn_classtype(setter)), decltype(match_memfn_argtype(setter)), setter, getter>
struct C
{
private:
int hiddenData;
protected:
void setInt(const int& data) { hiddenData = data; std::cout << "setter used\n"; }
const int& getInt() const { std::cout << "getter used\n"; return hiddenData; }
public:
C() : myInt(this), hiddenData(5) {}
property(&C::setInt,&C::getInt) myInt;
};
int main(void)
{
C c;
std::cout << "c.myInt = " << c.myInt << '\n';
c.myInt = -1;
std::cout << "c.myInt = " << c.myInt << '\n';
return 0;
}尽管VC++ 2010确实适用于非常简单的match_memfn_classtype使用,但它在所有变体上都令人窒息。
提交错误报告(请投赞成票):
C++ compiler loses member-ness of pointer-to-member-function during template deduction, causes ICE
微软更新了错误报告,称他们已经找到了修复方法。
发布于 2011-07-04 17:57:24
,我想知道我们是否可以做些什么,从两个函数指针的属性需求中提取类类型。
不怎么有意思。即使使用元编程类型的特征,也无法知道成员指针的类类型是什么。
哦,这些是成员指针,不是函数指针。它们不是一回事。
https://stackoverflow.com/questions/6569716
复制相似问题