"Function template" problem

Discussion in 'C++' started by Lionel B, Sep 30, 2004.

  1. Lionel B

    Lionel B Guest

    Greetings,

    I cannot figure out why the following code does not compile:

    --- BEGIN CODE ---

    typedef double ftype(double);

    struct A
    {
    double x;

    template<ftype F>
    double eval()
    {
    return F(x);
    }
    };

    template<ftype F>
    struct B
    {
    A a;

    double foo()
    {
    return a.eval<F>();
    }
    };

    --- END CODE ---

    It generates the error message:
    scratch.cpp: In member function `double B<F>::foo()':
    scratch.cpp:23: error: parse error before `;' token

    [ line 23 being return a.eval<F>(); ]

    Compiler gcc (GCC) 3.3.3 (cygwin special) on Win2K

    Any help appreciated,

    --
    Lionel B
     
    Lionel B, Sep 30, 2004
    #1
    1. Advertising

  2. "Lionel B" <> schrieb im Newsbeitrag
    news:...
    > Greetings,
    >
    > I cannot figure out why the following code does not compile:
    >
    > --- BEGIN CODE ---
    >
    > typedef double ftype(double);




    what you do is:
    template <double f> double func(f x);
    But: You don't want to give the type of the template argument, since
    it's a "template".
    So, use

    template<class C> double fkt(C argC);



    >
    > struct A
    > {
    > double x;
    >
    > template<ftype F>
    > double eval()
    > {
    > return F(x);
    > }
    > };
    >
    > template<ftype F>
    > struct B
    > {
    > A a;
    >
    > double foo()
    > {
    > return a.eval<F>();
    > }
    > };
    >
    > --- END CODE ---
    >
    > It generates the error message:
    > scratch.cpp: In member function `double B<F>::foo()':
    > scratch.cpp:23: error: parse error before `;' token
    >
    > [ line 23 being return a.eval<F>(); ]
    >
    > Compiler gcc (GCC) 3.3.3 (cygwin special) on Win2K
    >
    > Any help appreciated,
    >
    > --
    > Lionel B
     
    Gernot Frisch, Sep 30, 2004
    #2
    1. Advertising

  3. Lionel B

    Guest

    Gernot Frisch wrote:
    > "Lionel B" <> schrieb im Newsbeitrag
    > news:...
    > > Greetings,
    > >
    > > I cannot figure out why the following code does not compile:
    > >
    > > --- BEGIN CODE ---
    > >
    > > typedef double ftype(double);

    >
    > what you do is:
    > template <double f> double func(f x);
    > But: You don't want to give the type of the template argument, since
    > it's a "template".
    > So, use
    >
    > template<class C> double fkt(C argC);


    Apologies if I'm being obtuse, but I don't understand this reply at all
    :(

    --
    Lionel B
     
    , Sep 30, 2004
    #3
  4. Lionel B

    Markus Moll Guest

    Hi

    Lionel B wrote:

    > double foo()
    > {
    > return a.eval<F>();
    > }
    > };
    >
    > --- END CODE ---
    >
    > It generates the error message:
    > scratch.cpp: In member function `double B<F>::foo()':
    > scratch.cpp:23: error: parse error before `;' token


    This is one of the rare cases where you need to tell the compiler that the
    name eval is a member template (it's a dependent name), otherwise the '<'
    is considered "less than".

    double foo()
    {
    return a.template eval<F>();
    }

    Markus
     
    Markus Moll, Sep 30, 2004
    #4
  5. <> schrieb im Newsbeitrag
    news:...
    > Gernot Frisch wrote:
    >> "Lionel B" <> schrieb im Newsbeitrag
    >> news:...
    >> > Greetings,
    >> >
    >> > I cannot figure out why the following code does not compile:
    >> >
    >> > --- BEGIN CODE ---
    >> >
    >> > typedef double ftype(double);

    >>
    >> what you do is:
    >> template <double f> double func(f x);
    >> But: You don't want to give the type of the template argument,
    >> since
    >> it's a "template".
    >> So, use
    >>
    >> template<class C> double fkt(C argC);

    >
    > Apologies if I'm being obtuse, but I don't understand this reply at
    > all
    > :(


    er... do you know what templates are and what you use them for?

    What are you trying to do? I don't get a clue from your source code,
    that I have in common with your compiler ;)
     
    Gernot Frisch, Sep 30, 2004
    #5
  6. Lionel B

    Lionel B Guest

    Markus Moll wrote:
    > Hi
    >
    > Lionel B wrote:
    >
    > > double foo()
    > > {
    > > return a.eval<F>();
    > > }
    > > };
    > >
    > > --- END CODE ---
    > >
    > > It generates the error message:
    > > scratch.cpp: In member function `double B<F>::foo()':
    > > scratch.cpp:23: error: parse error before `;' token

    >
    > This is one of the rare cases where you need to tell the compiler

    that the
    > name eval is a member template (it's a dependent name), otherwise the

    '<'
    > is considered "less than".
    >
    > double foo()
    > {
    > return a.template eval<F>();
    > }


    Wow, I wouldn't have got that in a million years... never seen that
    syntax before.

    Many thanks,

    --
    Lionel B
     
    Lionel B, Sep 30, 2004
    #6
  7. Lionel B

    Lionel B Guest

    Gernot Frisch wrote:
    > <> schrieb im Newsbeitrag
    > news:...
    > > Gernot Frisch wrote:
    > >> "Lionel B" <> schrieb im Newsbeitrag
    > >> news:...
    > >> > Greetings,
    > >> >
    > >> > I cannot figure out why the following code does not compile:
    > >> >
    > >> > --- BEGIN CODE ---
    > >> >
    > >> > typedef double ftype(double);
    > >>
    > >> what you do is:
    > >> template <double f> double func(f x);
    > >> But: You don't want to give the type of the template argument,
    > >> since
    > >> it's a "template".
    > >> So, use
    > >>
    > >> template<class C> double fkt(C argC);

    > >
    > > Apologies if I'm being obtuse, but I don't understand this reply at


    > > all
    > > :(

    >
    > er... do you know what templates are and what you use them for?


    Yes.

    > What are you trying to do? I don't get a clue from your source code,
    > that I have in common with your compiler ;)


    I am implementing Todd Veldhuizen's "Pointer-to-function as a template
    parameter" scheme to inline callbacks in extensive loops. See
    http://osl.iu.edu/~tveldhui/papers/techniques/ (Section 1.4) for the
    rationale behind the technique.

    My sample code is a "minimalist example" of the real problem which
    arose when trying to build a template class (my class B above) with a
    function (my function B::foo) that calls the member template function
    (my A::eval) for a member object (my B::a). I hope this is clear(ish).

    The (rather esoteric) solution I was looking for was supplied by Markus
    Moll in a previous post.

    Regards,

    --
    Lionel B
     
    Lionel B, Sep 30, 2004
    #7
  8. > return a.template eval<F>();

    WTF? Never seen C++ like this before. Now, this is totally insane.
    Let me get this in my hamster brain:
    F is a function of type: double()(double).
    And class A's got a member:
    template<ftype F> double eval();

    Now, class B has a function that will call a 'eval' of an 'A' object
    er... No. I'm too stupid. Honestly, can anyone please give me a like
    to what you did here?
     
    Gernot Frisch, Sep 30, 2004
    #8
  9. Lionel B

    Sharad Kala Guest

    "Gernot Frisch" <> wrote in message

    > Now, class B has a function that will call a 'eval' of an 'A' object
    > er... No. I'm too stupid. Honestly, can anyone please give me a like
    > to what you did here?


    I think Markus gave the explanation. You may want to read Josuttis and
    Vandevoorde's book on Templates - "C++ Templates: The Complete Guide", they
    cover many such tips and traps in their book.

    Sharad
     
    Sharad Kala, Sep 30, 2004
    #9
  10. Gernot Frisch wrote in news: in
    comp.lang.c++:

    >> return a.template eval<F>();

    >
    > WTF? Never seen C++ like this before. Now, this is totally insane.
    > Let me get this in my hamster brain:
    > F is a function of type: double()(double).
    > And class A's got a member:
    > template<ftype F> double eval();
    >
    > Now, class B has a function that will call a 'eval' of an 'A' object
    > er... No. I'm too stupid. Honestly, can anyone please give me a like
    > to what you did here?
    >
    >


    Well the ".template" is only required here because of a compiler error,
    with a conforming compiler, you can write:

    return a.eval< F >();

    However if 'a' was dependant on a template paramiter (it isn't in the
    OP's code) the the compiler needs to be told that object a's eval
    member is a template member, otherwise the compiler treats the
    above as:

    return ((a.eval) < F ) > ();

    In this case a syntax error ! however:

    struct A
    {
    template < int N >
    void eval( int a );
    };

    struct B
    {
    int eval;
    }

    template < typename T >
    void example( T t )
    {
    t.eval < 10 > ( 13 );
    };

    Substitute A and B for T in example() and you can see that without
    the .template both could (but don't) compile with completely
    different symantics.

    example() is designed to work with B style object's, i.e. with
    a simple eval member.

    template < typename T >
    void another( T t )
    {
    t.template eval < 10 > ( 13 );
    };

    another() above is designed to work with A style object's, i.e.
    with a template member function eval.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Sep 30, 2004
    #10
  11. Lionel B

    Markus Moll Guest

    Hi

    Rob Williscroft wrote:

    > Well the ".template" is only required here because of a compiler error,
    > with a conforming compiler, you can write:
    >
    > return a.eval< F >();


    Okay, I suspected that (there really is no obvious reason for .template),
    but section 14 did a great job of confusing me... ;-)

    Markus
     
    Markus Moll, Sep 30, 2004
    #11
    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. Dave
    Replies:
    4
    Views:
    7,677
    pdixtl
    Jun 4, 2010
  2. Yueh-Wei Hu
    Replies:
    0
    Views:
    448
    Yueh-Wei Hu
    May 23, 2004
  3. Robert Allan Schwartz
    Replies:
    1
    Views:
    399
    Victor Bazarov
    Aug 9, 2004
  4. Replies:
    2
    Views:
    543
  5. avasilev
    Replies:
    4
    Views:
    578
    avasilev
    Dec 22, 2011
Loading...

Share This Page