"Function template" problem

L

Lionel B

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,
 
G

Gernot Frisch

Lionel B said:
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,
 
G

google

Gernot said:
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
:(
 
M

Markus Moll

Hi

Lionel said:
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
 
G

Gernot Frisch

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 ;)
 
L

Lionel B

Markus said:
Hi



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,
 
L

Lionel B

Gernot said:
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,
 
G

Gernot Frisch

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?
 
S

Sharad Kala

Gernot Frisch said:
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
 
R

Rob Williscroft

Gernot Frisch wrote in in
comp.lang.c++:
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.
 
M

Markus Moll

Hi

Rob said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top