Boost logo

Boost :

From: David B. Held (dheld_at_[hidden])
Date: 2003-02-06 16:35:10


"Trey Jackson" <tjackson_at_[hidden]> wrote in message
news:qkxy94t2kfb.fsf_at_dlxc0015.pdx.intel.com...
> [...]
> I was unable to get that to compile.
> Linux gcc 3.2.1, or HP aCC: HP ANSI C++ B3910B X.03.32
>
> The linux error gave me a syntax error before the '{' token on the lines
> declaring 'insertCode' in both SubstituteHello and SubstituteGoodbye.

That's correct.

> ,----------------
> | #include <iostream>
> |
> | using std::cout; using std::endl;
> |
> | template<class ToBeSubstituted>
> | struct userClass {
> | void function() {
> | // code is inserted here, not a function call to a member of
> | ToBeSubstituted::insertCode();
> | }
> | };
> |
> | struct SubstituteHello {
> | typedef void fn();
> |
> | static fn insertCode {
> | cout << "hello world" << endl;
> | }
> | };

You can use a typedef to declare a function, but not to define one.
This code should actually be:

static void insertCode() { ...

> | struct SubstituteGoodbye {
> | typedef void fn();
> |
> | static fn insertCode {
> | cout << "goodbye world" << endl;
> | }
> | };

And here.

> [...]
> This compiles, runs with undesired behavior;
> ,----------------
> | struct SubstituteHello {
> | typedef void fn();
> |
> | static void insertCode()
> | {
> | dummy d;
> | cout << "Hello World" << endl;
> | }

That's pretty much how you have to do it.

> | // typedef void fn();
> | // static fn insertCode {
> | // cout << "hello world" << endl;
> | // }
> | };
> `----------------
> [...]

Dave


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk