How to use the template member function of a template in the memberfunction of another template clas

Discussion in 'C++' started by Peng Yu, Oct 25, 2008.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    I have the following program. The error line is commented. I want to
    call _a's member function 'doit' with the template argument of
    function<T>. Would you please what is the correct way to do it?

    Thanks,
    Peng

    #include <iostream>

    template <typename T>
    struct function {
    T operator()() const {
    return T();
    }
    };

    template <typename T>
    struct A {
    template <typename F>
    T doit() {
    return F()();
    }
    };

    template <typename T>
    struct B {
    public:
    T doit() {
    return _a.doit<function<T> >();//error
    }
    private:
    A<T> _a;
    };


    int main() {
    A<double> a;
    std::cout << a.doit<function<double> >() << std::endl;
    }
    Peng Yu, Oct 25, 2008
    #1
    1. Advertising

  2. Peng Yu

    Kai-Uwe Bux Guest

    Re: How to use the template member function of a template in the member function of another template class?

    Peng Yu wrote:

    > I have the following program. The error line is commented. I want to
    > call _a's member function 'doit' with the template argument of
    > function<T>.

    [snip]
    >
    > #include <iostream>
    >
    > template <typename T>
    > struct function {
    > T operator()() const {
    > return T();
    > }
    > };
    >
    > template <typename T>
    > struct A {
    > template <typename F>
    > T doit() {
    > return F()();
    > }
    > };
    >
    > template <typename T>
    > struct B {
    > public:
    > T doit() {
    > return _a.doit<function<T> >();//error


    Try:

    return _a.template doit< function<T> >();

    > }
    > private:
    > A<T> _a;
    > };
    >
    >
    > int main() {
    > A<double> a;
    > std::cout << a.doit<function<double> >() << std::endl;
    > }



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 25, 2008
    #2
    1. Advertising

  3. Peng Yu

    annamalai Guest

    Re: How to use the template member function of a template in themember function of another template class?

    On Oct 24, 9:54 pm, Peng Yu <> wrote:
    > template <typename T>
    > struct B {
    > public:
    > T doit() {
    > return _a.doit<function<T> >();//error
    > }
    > private:
    > A<T> _a;
    >
    > };


    The only way I was able to make it work was to make the B::doit() as a
    template member function. I don't know enough to explain why the
    compiler is not able to instantiate function<T> as used above. Maybe
    someone with more knowledge will be able to explain it. Here is the
    complete program.

    #include <iostream>

    template <typename T>
    struct function {
    T operator()() const {
    std::cout << "T function::eek:perator() called" << std::endl;
    return T();
    }

    };

    template <typename T>
    struct A {
    template <typename F>
    T doit() {
    std::cout << "T A::doit() called" << std::endl;
    return F()();
    }

    };

    template <typename T>
    struct B {
    public:
    template <typename P>
    T doit() {
    std::cout << "T B::doit() called" << std::endl;
    return _a.doit<P>();
    }
    private:
    A<T> _a;

    };

    int main() {
    A<double> a;
    double d = a.doit< function<double> >();
    B<double> b;
    double e = b.doit< function<double> >();
    }

    --
    Copyright of Wedding Album Must Belong to Family (The Producers)
    http://tinyurl.com/d0253082-copyright
    annamalai, Oct 26, 2008
    #3
  4. Re: How to use the template member function of a template in themember function of another template class?

    > On Oct 24, 9:54 pm, Peng Yu <> wrote:
    >> template <typename T>
    >> struct B {
    >> public:
    >> T doit() {
    >> return _a.doit<function<T> >();//error


    doit is a dependent name, so you have to tell the compiler, when it is a
    typename or template. Otherwise, the compiler assumes a non-typename and
    non-template member. Insert the template keyword before the function name:

    return _a.template doit<function<T> >();

    The FAQ explains this for the typename keyword, but it also applies to
    templates:
    http://www.parashift.com/c -faq-lite/templates.html#faq-35.18

    >> }
    >> private:
    >> A<T> _a;
    >>
    >> };


    --
    Thomas
    Thomas J. Gritzan, Oct 26, 2008
    #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. Newsgroup - Ann
    Replies:
    5
    Views:
    603
    John Carson
    Jul 30, 2003
  2. PKH
    Replies:
    2
    Views:
    376
  3. lord trousers
    Replies:
    4
    Views:
    647
    James Kanze
    Jan 10, 2008
  4. Replies:
    8
    Views:
    297
    James Kanze
    Mar 2, 2008
  5. Ulrich Eckhardt

    decorating a memberfunction

    Ulrich Eckhardt, Jun 2, 2010, in forum: Python
    Replies:
    1
    Views:
    240
Loading...

Share This Page