|
Boost : |
From: Trey Jackson (tjackson_at_[hidden])
Date: 2003-02-06 16:01:12
>I don't think you need that document. Whatever could be:
>
> typedef void fn(); static fn
>
>In other words, make them static functions. userClass::function just needs
>to add a couple of parens after "insertCode" ;-)
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.
,----------------
| #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;
| }
| };
|
| struct SubstituteGoodbye {
| typedef void fn();
|
| static fn insertCode {
| cout << "goodbye world" << endl;
| }
| };
|
| int main()
| {
| userClass<SubstituteHello> u1;
| u1.function(); // prints 'hello world'
| userClass<SubstituteGoodbye> u2;
| u2.function(); // prints 'goodbye world'
|
| return 0;
|
| }
`----------------
I tried a similar vesion, only without the 'typedef void fn();'
To me, the two chunks of code appear the same. How are the two chunks
semantically different? (I imagine they are different or you wouldn't
have introduced the extra 'typedef void fn()').
This compiles, runs with undesired behavior;
,----------------
| struct SubstituteHello {
| typedef void fn();
|
| static void insertCode()
| {
| dummy d;
| cout << "Hello World" << endl;
| }
|
| // typedef void fn();
| // static fn insertCode {
| // cout << "hello world" << endl;
| // }
| };
`----------------
But the generated code appeared to resulted in the destructor of d
being called being before any code in the userClass method 'function'
gets evaluated.
Is that the expected behavior? I'd hoped for truly in-lined code (for
lack of a better term), w/out the lexical scope of 'insertCode' being
introduced.
This is all going back to the desire to factor out the mutex locking,
and wanting to insert (or not) a simple line:
boost::mutex::scoped_lock l(mutex_)
and not have the lock released until the end of the method 'function'.
Apologies if this seems trivial.
The help is appreciated.
TJ
-- Trey Jackson tjackson_at_[hidden] "Every day, the hummingbird eats its own weight in food. You may wonder how it weighs the food. It doesn't. It just eats another hummingbird." -- unknown
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk