473,597 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 1765

"Lionel B" <go****@lionelb .com> schrieb im Newsbeitrag
news:e1******** *************** ***@posting.goo gle.com...
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.goo gle.com...
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.goo glegroups.com.. .
Gernot Frisch wrote:
"Lionel B" <go****@lionelb .com> schrieb im Newsbeitrag
news:e1******** *************** ***@posting.goo gle.com...
> 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.goo glegroups.com.. .
Gernot Frisch wrote:
"Lionel B" <go****@lionelb .com> schrieb im Newsbeitrag
news:e1******** *************** ***@posting.goo gle.com...
> 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4049
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). // Integer calculation (fast) int iround(const double x) { return (x>=0 ? static_cast<int>(x + 0.5) : static_cast<int>(x + 0.5) } // Floating point calculation (slow) double dround(const double x) {
1
1561
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 development. Could you give your points, corrections and ideas about template below? And are elemens ids available in form or another in all browsers or was there some which accpets only element names? And was it that ancient netscape only gives form...
5
3584
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 AutoComPtr { ... The following errors were shown:
2
2008
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. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
2
1582
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 page presentations, it works well. However, I have a couple of screen scrape / import routines that can take minutes, even hours, to complete. I'd like to have some way of outputting my template first to the browser, and then within the...
9
3325
by: Kobe | last post by:
Is there any difference in: template <class T> vs. template <typename T> ?
28
2239
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 modified: " + document.lastModified); --> </script>
28
2834
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 apply. Can somebody tell me why? Thanks, Jess
3
1473
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 // generates operator T*() const { return 0; } // operator T* for }; // all types T; each // function returns // the null pointer
0
7894
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8262
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6706
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2409
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1497
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.