I am trying to create alternative names for the function call numberedFunction when it has certain values as below
template< typename T >
class X
{
public:
X() : single( std::bind( &X::numberedFunction, *this, 1 ) ),
halfDozen( std::bind( &X::numberedFunction, *this, 6 ) )
{ ... }
T numberedFunction( unsigned int i ) { ... }
const std::function< T() >& single;
const std::function< T() >& halfDozen;
};
But this code is not correct (segfaults when I try to use any of the specially named functions). Is there an issue with using this the way I am in the initialization list (e.g., is this not guarenteed to be well-formed at the time I am accessing it there)? Something else (obvious)? Is there a better way to do what I am trying to do (I feel like there almost definitely is)?
Your members are references to
const, but you are initializing them from a temporary in the constructor (assuming thebindexpressions in your real code aren’t nonsensical). As soon as the construction is done they are invalid. Is this really what you intended?Maybe this is what you want do do (using psychic powers here):
Notice that I’m binding to
this, not*this. This avoids a copy, but may not be what you want.