Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   forwarding templates (http://www.velocityreviews.com/forums/t459678-forwarding-templates.html)

Thomas 01-06-2007 11:27 AM

forwarding templates
 
Hello,

I need a template class with a type T and a function INIT. For the default
case the INIT-function-pointer should point to the internal member function
init. But since the template parameters depend on the class member function
( see xx in template<class T, void (*INIT)(void)=C_C<T, xx>::init > ) I
don't know how to overcome this problem. Can onybody give me a hint.

Thank You, Thomas



template<class T, void (*INIT)(void)> class C_C;

template<class T, void (*INIT)(void)=C_C<T, xx>::init >

class C_C

{

protected:

static void init(void){ cout << endl << "C_C::init()"; return; }

protected:

T val;

public:

C_C( void ){ INIT(); cout << endl << "+C_C"; return; }

virtual ~C_C( void ) { cout << endl << "-C_C"; return; }

};







Ondra Holub 01-06-2007 01:37 PM

Re: forwarding templates
 
Thomas napsal:
> Hello,
>
> I need a template class with a type T and a function INIT. For the default
> case the INIT-function-pointer should point to the internal member function
> init. But since the template parameters depend on the class member function
> ( see xx in template<class T, void (*INIT)(void)=C_C<T, xx>::init > ) I
> don't know how to overcome this problem. Can onybody give me a hint.
>
> Thank You, Thomas
>
>
>
> template<class T, void (*INIT)(void)> class C_C;
>
> template<class T, void (*INIT)(void)=C_C<T, xx>::init >
>
> class C_C
>
> {
>
> protected:
>
> static void init(void){ cout << endl << "C_C::init()"; return; }
>
> protected:
>
> T val;
>
> public:
>
> C_C( void ){ INIT(); cout << endl << "+C_C"; return; }
>
> virtual ~C_C( void ) { cout << endl << "-C_C"; return; }
>
> };


Hi. You need to know init function already before class C_C definition.
Try it this way:

#include <iostream>

template<typename T>
struct C_C_initializer
{
static void init(T& t)
{
std::cout << "C_C_initializer::init(T& t)\n";
}
};

template<class T, void (*INIT)(T&) = C_C_initializer<T>::init>
class C_C
{
protected:
T val;

public:
C_C()
{
INIT(val);
std::cout << "+C_C\n";
}

virtual ~C_C()
{
std::cout << "-C_C\n";
}

private:
};

// Custom initializer
void MyInit(int& i)
{
std::cout << "MyInit\n";
}

int main()
{
C_C<int> instance;
C_C<int, MyInit> instance2;
}

In initializer is added some reference to value to be initialized.
Place there whatever you want.


kwikius 01-06-2007 01:37 PM

Re: forwarding templates
 

Thomas wrote:
> Hello,
>
> I need a template class with a type T and a function INIT. For the default
> case the INIT-function-pointer should point to the internal member function
> init. But since the template parameters depend on the class member function
> ( see xx in template<class T, void (*INIT)(void)=C_C<T, xx>::init > ) I
> don't know how to overcome this problem. Can onybody give me a hint.


There are probably various ways. This one uses a dummy default argument
and a traits class.

regards
Andy Little
//------------------

#include <iostream>

// dummy function
void default_init(){}

// use as default
template<class T, void (*INIT)(void) = default_init > class C_C;

//traits class
template <typename C>
struct init_traits;

// unspecialised use for no default
template <
class T,
void (*INIT)(void)
>

struct init_traits< C_C<T,INIT> >
{
static void (*init_function)();
};

//specialsie use for default
template < class T>
struct init_traits< C_C<T,default_init> >
{
static void (*init_function)();
};

// need to declare...
template<
class T,
void (*INIT)(void)
>

void (*init_traits<C_C<T,INIT> >::init_function)()= INIT;

template<
class T
>

void (*init_traits<C_C<T,default_init> >::init_function)()=
C_C<T,default_init>::init;


template<
class T,
void (*INIT)(void)
>

class C_C {
public:
static void init(void){ std::cout << "C_C::init()\n"; return; }

T val;

C_C( void ){ init_traits<C_C>::init_function(); }
virtual ~C_C( void ) { std::cout << "-C_C\n"; return; }
};

void other_init(){std::cout << "other init\n";}

int main()
{
C_C<int> x;
C_C<int,other_init> xx;
}


Thomas 01-06-2007 02:08 PM

Re: forwarding templates
 
Hello,

many thanks to Ondra and Andy for their directly answer.

My primary goal was to "include" the helper init functions resp. init
classes (C_C_initializer, default_init ) as a member function in C_C. With
respect to the comments of Ondra and Andy it seems, that there is no way to
do that.

Best regards, Thomas




"Thomas" <Thomas@Schmid-Zettler.de> schrieb im Newsbeitrag
news:eno12t$4jc$1@online.de...
> Hello,
>
> I need a template class with a type T and a function INIT. For the
> default
> case the INIT-function-pointer should point to the internal member
> function
> init. But since the template parameters depend on the class member
> function
> ( see xx in template<class T, void (*INIT)(void)=C_C<T, xx>::init > ) I
> don't know how to overcome this problem. Can onybody give me a hint.
>
> Thank You, Thomas
>
>
>
> template<class T, void (*INIT)(void)> class C_C;
>
> template<class T, void (*INIT)(void)=C_C<T, xx>::init >
>
> class C_C
>
> {
>
> protected:
>
> static void init(void){ cout << endl << "C_C::init()"; return; }
>
> protected:
>
> T val;
>
> public:
>
> C_C( void ){ INIT(); cout << endl << "+C_C"; return; }
>
> virtual ~C_C( void ) { cout << endl << "-C_C"; return; }
>
> };
>
>
>
>
>
>





All times are GMT. The time now is 10:09 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.