forwarding templates

Discussion in 'C++' started by Thomas, Jan 6, 2007.

  1. Thomas

    Thomas Guest

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

    };
     
    Thomas, Jan 6, 2007
    #1
    1. Advertising

  2. Thomas

    Ondra Holub Guest

    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.
     
    Ondra Holub, Jan 6, 2007
    #2
    1. Advertising

  3. Thomas

    kwikius Guest

    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;
    }
     
    kwikius, Jan 6, 2007
    #3
  4. Thomas

    Thomas Guest

    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" <> schrieb im Newsbeitrag
    news:eno12t$4jc$...
    > 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; }
    >
    > };
    >
    >
    >
    >
    >
    >
     
    Thomas, Jan 6, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred
    Replies:
    1
    Views:
    635
    Neredbojias
    Sep 26, 2005
  2. JKop
    Replies:
    3
    Views:
    522
  3. recover
    Replies:
    2
    Views:
    858
    recover
    Jul 25, 2006
  4. Scott Meyers
    Replies:
    20
    Views:
    1,156
    itaj sherman
    Mar 16, 2011
  5. Andrew Tomazos
    Replies:
    5
    Views:
    632
Loading...

Share This Page