W
Werner
Hi All,
I've found an interesting use for lambdas in member initializer
lists, and I've wondered if this would be frowned upon from a style
perspective:
Plot:lot()
: curve_( []( Plot& plot )
{
std::unique_ptr<Curve> curve( new Curve );
curve->setZ_Order( 20 );
curve->associate( plot );
return curve;
}( *this ),
etc...
{
}
as opposed to:
Plot:lot()
: curve_( new Curve )
{
//Initialization seperated from creation...
initialiseCurve();
}
I like the idea of using the lambda (or a creation function)
because if initialization fails, creation should too, and then
it would not benefit to create the rest of the object.
OTOH, inter-dependencies may exist between objects:
Widget::Widget()
: plot_(...),
curve_( []( Plot& plot ) //Curve depends on plot...
{
std::unique_ptr<Curve> curve( new Curve );
curve->setZ_Order( 20 );
curve->associate( plot );
return curve;
}( *plot_ ),
etc...
{
}
This creates the risk that plot does not exist, especially if
member declaration order is different to the order in the
member initializer list. On the other hand most compilers
do warn about this kind of thing.
Any thoughts on this style issue?
Kind Regards,
Werner
I've found an interesting use for lambdas in member initializer
lists, and I've wondered if this would be frowned upon from a style
perspective:
Plot:lot()
: curve_( []( Plot& plot )
{
std::unique_ptr<Curve> curve( new Curve );
curve->setZ_Order( 20 );
curve->associate( plot );
return curve;
}( *this ),
etc...
{
}
as opposed to:
Plot:lot()
: curve_( new Curve )
{
//Initialization seperated from creation...
initialiseCurve();
}
I like the idea of using the lambda (or a creation function)
because if initialization fails, creation should too, and then
it would not benefit to create the rest of the object.
OTOH, inter-dependencies may exist between objects:
Widget::Widget()
: plot_(...),
curve_( []( Plot& plot ) //Curve depends on plot...
{
std::unique_ptr<Curve> curve( new Curve );
curve->setZ_Order( 20 );
curve->associate( plot );
return curve;
}( *plot_ ),
etc...
{
}
This creates the risk that plot does not exist, especially if
member declaration order is different to the order in the
member initializer list. On the other hand most compilers
do warn about this kind of thing.
Any thoughts on this style issue?
Kind Regards,
Werner