Re: The joy (?) of C++98 argument forwarding
On 04.05.2010 14:56, * DeMarcus:
Alf P. Steinbach wrote:
Consider ...
<code>
// Copyright (c) Alf P. Steinbach, 2010.
#include "_config.h"
#include <progrock/cppx/pointers/Shared.h>
#include <iostream>
namespace {
using namespace progrock;
cppx::Size count = 0;
class Foo
{
protected:
~Foo()
{
using namespace std;
--count;
cout << "Foo destroyed" << endl;
}
public:
Foo()
{
using namespace std;
cout << "Foo constructed" << endl;
++count;
}
Foo( int x )
{
using namespace std;
cout << "Foo constructed with arg " << x << endl;
++count;
}
Foo( int x, char const s[] )
{
using namespace std;
cout << "Foo constructed with args " << x << " and \"" << s << "\"" <<
endl;
++count;
}
};
void test()
{
using namespace cppx;
Shared< Foo > r1 = newObject();
assert( count == 1 );
Shared< Foo > r2( r1 );
assert( count == 1 );
Shared< Foo > r3( newObject(), args( 42 ) );
assert( count == 2 );
Shared< Foo > r4( newObject(), args( 42, "blah blah" ) );
assert( count == 3 );
r4 = r2;
assert( count == 2 );
r3 = r2;
assert( count == 1 );
r3 = r3;
assert( count == 1 );
}
}; // namespace anon
void testShared()
{
test();
assert( count == 0 );
}
</code>
Is the notation in the 'test' routine OK, or would it be better (in
some sense) with some macro, e.g. like
Shared< Foo > r( CPPX_NEW_SHARED( 42, "blah blah" ) );
?
At least consider CPPX_NEW_SHARED_FOO( 42, "blah blah" ). Otherwise the
macro name would be as non-intuitive as
CPPX_NEW_SHARED( PI, 21.45, time() )
Well for the example above the type is implicit in the declaration, so it would
be redundant to repeat it there.
On the other hand you have a point -- I didn't think of it -- for an actual
argument in a routine call.
On the third hand, we usually don't specify arguments there for value arguments.
Can't you do the wrapper with a template instead of a macro?
Not sure what you mean; the Shared shared pointer type is templated, as the
example usage code shows.
Cheers,
- Alf