forwarding templates

T

Thomas

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; }

};
 
O

Ondra Holub

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

kwikius

Thomas said:
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 Tvoid (*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;
}
 
T

Thomas

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top