Template Function Format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • miben@miben.net

    #1

    Template Function Format

    OK, this is tricky. I have a function inside a template that returns a
    pointer to a function whose space is declared as, "void
    Function(void)" . To declare this, i use:

    template <typename Class>
    class MyClass {
    public:
    void SetOnClose(void (Class::*lptnOn Close)(void));
    void (Class::*GetOnC lose(void))(voi d);
    };

    How do you declare the GetOnClose function, above, outside of the
    templete?
    Currently, i just use the defined function above to do what I need. I
    can declare the SetOnClose function outside of the template just fine.

    template <typename Class>
    class MyClass {
    public:
    void SetOnClose(void (Class::*lptnOn Close)(void));
    void (Class::*GetOnC lose(void))(voi d) {
    do stuff here
    }
    };

    template <typename Class>
    void Form<Class>::Se tOnClose(void (Class::*lptnOn Close)(void)) {
    do stuff here
    }

  • n2xssvv g02gfr12930

    #2
    Re: Template Function Format

    miben@miben.net wrote:[color=blue]
    > OK, this is tricky. I have a function inside a template that returns a
    > pointer to a function whose space is declared as, "void
    > Function(void)" . To declare this, i use:
    >
    > template <typename Class>
    > class MyClass {
    > public:
    > void SetOnClose(void (Class::*lptnOn Close)(void));
    > void (Class::*GetOnC lose(void))(voi d);
    > };
    >
    > How do you declare the GetOnClose function, above, outside of the
    > templete?
    > Currently, i just use the defined function above to do what I need. I
    > can declare the SetOnClose function outside of the template just fine.
    >
    > template <typename Class>
    > class MyClass {
    > public:
    > void SetOnClose(void (Class::*lptnOn Close)(void));
    > void (Class::*GetOnC lose(void))(voi d) {
    > do stuff here
    > }
    > };
    >
    > template <typename Class>
    > void Form<Class>::Se tOnClose(void (Class::*lptnOn Close)(void)) {
    > do stuff here
    > }
    >[/color]
    Your code is riddled with errors, but I guess this ia the type of thing
    you intended:

    template <typename Class>
    class MyClass
    {
    private:
    void (MyClass::*pfOn Close)(void);
    public:
    void SetOnClose(void (MyClass::*inp) (void))
    {
    pfOnClose = inp;
    }
    void close1(void);
    void close2(void);
    void close(void)
    {
    (this->*pfOnClose)( );
    }
    };

    template <typename Class>
    void MyClass<Class>: :close1(void)
    {
    std::cout << "close 1" << std::endl;
    }

    template <typename Class>
    void MyClass<Class>: :close2(void)
    {
    std::cout << "close 2" << std::endl;
    }

    void SomeFunction(vo id)
    {
    MyClass<int> Tmp;
    Tmp.SetOnClose( &MyClass<int>:: close1);
    Tmp.close();
    Tmp.SetOnClose( &MyClass<int>:: close2);
    Tmp.close();
    }

    JB

    Comment

    • John Harrison

      #3
      Re: Template Function Format

      n2xssvv g02gfr12930 wrote:[color=blue]
      > miben@miben.net wrote:
      >[color=green]
      >> OK, this is tricky. I have a function inside a template that returns a
      >> pointer to a function whose space is declared as, "void
      >> Function(void)" . To declare this, i use:
      >>
      >> template <typename Class>
      >> class MyClass {
      >> public:
      >> void SetOnClose(void (Class::*lptnOn Close)(void));
      >> void (Class::*GetOnC lose(void))(voi d);
      >> };
      >>
      >> How do you declare the GetOnClose function, above, outside of the
      >> templete?
      >> Currently, i just use the defined function above to do what I need. I
      >> can declare the SetOnClose function outside of the template just fine.
      >>
      >> template <typename Class>
      >> class MyClass {
      >> public:
      >> void SetOnClose(void (Class::*lptnOn Close)(void));
      >> void (Class::*GetOnC lose(void))(voi d) {
      >> do stuff here
      >> }
      >> };
      >>
      >> template <typename Class>
      >> void Form<Class>::Se tOnClose(void (Class::*lptnOn Close)(void)) {
      >> do stuff here
      >> }
      >>[/color]
      > Your code is riddled with errors, but I guess this ia the type of thing
      > you intended:
      >[/color]

      How is it riddled with errors? Apart from the misprint of Form for
      MyClass is looks fine (and compiles fine).

      john

      Comment

      • John Harrison

        #4
        Re: Template Function Format

        miben@miben.net wrote:[color=blue]
        > OK, this is tricky. I have a function inside a template that returns a
        > pointer to a function whose space is declared as, "void
        > Function(void)" . To declare this, i use:
        >
        > template <typename Class>
        > class MyClass {
        > public:
        > void SetOnClose(void (Class::*lptnOn Close)(void));
        > void (Class::*GetOnC lose(void))(voi d);
        > };
        >
        > How do you declare the GetOnClose function, above, outside of the
        > templete?
        > Currently, i just use the defined function above to do what I need. I
        > can declare the SetOnClose function outside of the template just fine.
        >
        > template <typename Class>
        > class MyClass {
        > public:
        > void SetOnClose(void (Class::*lptnOn Close)(void));
        > void (Class::*GetOnC lose(void))(voi d) {
        > do stuff here
        > }
        > };
        >
        > template <typename Class>
        > void Form<Class>::Se tOnClose(void (Class::*lptnOn Close)(void)) {
        > do stuff here
        > }
        >[/color]

        The following code compiler for me, but as soon as I add #include
        <iostream> I get an internal compiler error.

        This syntax is far too obscure, not only for anyone reading your code
        but also it seems for compilers. Be sensible and use a typedef.


        struct X
        {
        void close() {}
        };

        template <typename Class>
        class MyClass {
        public:
        void SetOnClose(void (Class::*lptnOn Close)(void));
        void (Class::*GetOnC lose(void))(voi d);
        };

        template <typename Class>
        void MyClass<Class>: :SetOnClose(voi d (Class::*lptnOn Close)(void)) {
        }

        template <typename Class>
        void (Class::*MyClas s<Class>::GetOn Close(void))(vo id) {
        return 0;
        }

        int main()
        {
        MyClass<X> x;
        x.SetOnClose(&X ::close);
        void (X::*ptr)(void) = x.GetOnClose();
        }

        john

        Comment

        • miben@miben.net

          #5
          Re: Template Function Format

          ya, i thought that is how i should do it:

          template <typename Class>
          void (Class::*MyClas s<Class>::GetOn Close(void))(vo id) {
          }

          I keep getting an internal compile error. typedef sounds like a better
          way to do this. It would also make the code much more readable.
          typedef inside the class?

          Comment

          • John Harrison

            #6
            Re: Template Function Format

            miben@miben.net wrote:[color=blue]
            > ya, i thought that is how i should do it:
            >
            > template <typename Class>
            > void (Class::*MyClas s<Class>::GetOn Close(void))(vo id) {
            > }
            >
            > I keep getting an internal compile error. typedef sounds like a better
            > way to do this. It would also make the code much more readable.
            > typedef inside the class?
            >[/color]

            I think so, this is what I did

            #include <iostream>

            struct X
            {
            void close() {}
            };

            template <typename Class>
            class MyClass {
            public:
            typedef void (Class::*closer )(void);
            void SetOnClose(clos er);
            closer GetOnClose(void );
            };

            template <typename Class>
            void MyClass<Class>: :SetOnClose(clo ser) {
            }

            template <typename Class>
            typename MyClass<Class>: :closer MyClass<Class>: :GetOnClose(voi d) {
            return 0;
            }

            int main()
            {
            MyClass<X> x;
            x.SetOnClose(&X ::close);
            MyClass<X>::clo ser func = x.GetOnClose();
            }

            BTW g++ 3.4.4 compiles both versions without problems.

            john

            Comment

            • miben@miben.net

              #7
              Re: Template Function Format

              template <typename Class>
              class MyClass {
              public:
              typedef void (Class::*closer )(void);
              closer GetOnClose(void );
              };

              template <typename Class>
              typename MyClass<Class>: :closer MyClass<Class>: :GetOnClose(voi d) {
              return 0;
              }


              Very nice job, john.

              Comment

              • miben@miben.net

                #8
                Re: Template Function Format

                Works great.

                template <typename Class>
                class Form: public Window {
                public:
                Form(void);
                ~Form(void);

                void operator =(const Form &frmForm);

                typedef void (Class::*m_tdOn Close)(void);
                private:
                m_tdOnClose m_lptnOnClose;
                };

                template <typename Class>
                typename Form<Class>::m_ tdOnClose Form<Class>::Ge tOnClose(void) {
                return m_lptnOnClose;
                }

                template <typename Class>
                void Form<Class>::Se tOnClose(m_tdOn Close lptnOnClose) {
                m_lptnOnClose = lptnOnClose;
                }

                Comment

                Working...