Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template of template c++?

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?

like image 561
Ali1S232 Avatar asked Jul 04 '11 09:07

Ali1S232


People also ask

Can template parameter be a template?

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.)

What is a template argument C++?

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 is meant by template in C?

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.

Are there templates C?

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.


1 Answers

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.

like image 167
Ise Wisteria Avatar answered Nov 10 '22 10:11

Ise Wisteria