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;
}
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?
A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)
In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
What Does Template Mean? A template is a C++ programming feature that permits function and class operations with generic types, which allows functionality with different data types without rewriting entire code blocks for each type.
The main type of templates that can be implemented in C are static templates. Static templates are created at compile time and do not perform runtime checks on sizes, because they shift that responsibility to the compiler.
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;
};
Incidentally, MSVC has its own property. Probably this is easier if it serves the purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With