467,915 Members | 1,084 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

"Function template" problem

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
Jul 22 '05 #1
  • viewed: 1525
Share:
10 Replies

"Lionel B" <go****@lionelb.com> schrieb im Newsbeitrag
news:e1**************************@posting.google.c om...
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

Jul 22 '05 #2
Gernot Frisch wrote:
"Lionel B" <go****@lionelb.com> schrieb im Newsbeitrag
news:e1**************************@posting.google.c om...
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

Jul 22 '05 #3
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

Jul 22 '05 #4

<go****@lionelb.com> schrieb im Newsbeitrag
news:10**********************@k26g2000oda.googlegr oups.com...
Gernot Frisch wrote:
"Lionel B" <go****@lionelb.com> schrieb im Newsbeitrag
news:e1**************************@posting.google.c om...
> 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 ;)

Jul 22 '05 #5
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

Jul 22 '05 #6
Gernot Frisch wrote:
<go****@lionelb.com> schrieb im Newsbeitrag
news:10**********************@k26g2000oda.googlegr oups.com...
Gernot Frisch wrote:
"Lionel B" <go****@lionelb.com> schrieb im Newsbeitrag
news:e1**************************@posting.google.c om...
> 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

Jul 22 '05 #7
> 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?
Jul 22 '05 #8

"Gernot Frisch" <Me@Privacy.net> 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
Jul 22 '05 #9
Gernot Frisch wrote in news:2s*************@uni-berlin.de 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/
Jul 22 '05 #10
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

Jul 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jef Driesen | last post: by
1 post views Thread by Perttu Pulkkinen | last post: by
5 posts views Thread by Jacky Yuk | last post: by
2 posts views Thread by Rudy Ray Moore | last post: by
9 posts views Thread by Kobe | last post: by
28 posts views Thread by fred.haab | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.