I just thought of experimenting with something today, a little variation on the named parameter idiom. It's a bit bloated, but I'm just messing around a little bit. Anyway, I came across a problem, my first guess is that this might not be allowed by the language in the first place - in which case I can use the boost preprocessor library to make a number of constructors, but I'd prefer that to be the last resort.
The troublesome bit of code is this pices:
struct NullParameter {
NullParameter() {}
void operator () ( void* o ) const
{}
};
class Texture
{
template< class P0 , class P1, class P2, class P3 >
Texture( const P0& p0 = NullParameter(),
const P1& p1 = NullParameter(),
const P2& p2 = NullParameter(),
const P3& p3 = NullParameter() )
: m_width(0),
m_height(0),
m_name(""),
m_useColorKey(false),
m_colorKey(0),
m_colorKeyAlpha(0)
{
p0(this);
p1(this);
p2(this);
p3(this);
// this->create();
}
};
It compiles fine if I privide arguments to all the parameters, but if it's not four, it complains about "does not take 3 arguments". So this would work:
Texture t( Texture::argSize( 500, 600 ),
Texture::argFilename( "happy" ),
Texture::argFormat( Texture::TF_Float32 ),
Texture::argColorKey( 0x0, 0x0 ) );
But this would not:
Texture t( Texture::argSize( 500, 600 ),
Texture::argFilename( "happy" ),
Texture::argFormat( Texture::TF_Float32 ) );
So is there a way to solve this problem without making multiple constructors? Incase you're wondering, Texture::argSize, Texture::argColorKey and the others are structures within the Texture class that overload operator () with a single parameter to a Texture*. The operator () overload is responsible for setting the private variables of the Texture object based on what was passed in at their time of construction: Eg: argColorKey would look like this:
class Texture
{
public:
//====
struct argColorKey {
//====
argColorKey( uint32 c, uint8 a ) : color(c), alpha(a)
{}
void operator () ( Texture* o ) const {
o->m_useColorKey = true;
o->m_colorKey = color;
o->m_colorKeyAlpha = alpha;
}
private:
uint32 color;
uint8 alpha;
};
};













