473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

"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
10 1744

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jef Driesen | last post by:
I need to implement a function to implement the rounding of floating point values. At the moment i have two different implementations, depending on the type of the return value (integer or double)....
1
by: Perttu Pulkkinen | last post by:
I would like to have a javasript/jscript function template/"framwork" instead of checking browsers by name. The shortness of script in my opinion is not the goal, but clearness and ease iof...
5
by: Jacky Yuk | last post by:
Hi all, I am new to c++ but using c for long time. Recently, I created a MFC GUI project by VC/C++ 6.0. Everything was fine until I wanted to use "template": template <typename T> class...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Brent | last post by:
Like many sites, mine has a standard "look" -- a template, if you will -- that visitors see on each page. I've tried to keep the code and HTML separate to the extent possible, and for most standard...
9
by: Kobe | last post by:
Is there any difference in: template <class T> vs. template <typename T> ?
28
by: fred.haab | last post by:
Not having server side scripting, I've been doing this for "last modified" tags on my pages: <div class="modified"> <script type="Text/JavaScript"> <!-- document.write("This page was last...
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
3
by: Aries Sun | last post by:
I am reading the book "Effective C++" Item 25 mentioned the following code: // a first cut at a class yielding NULL pointer objects class NullClass { public: template<class T ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.