Consider this piece of code creating, based on a condition, a different class instance through a std::make_shared. Note that the two possible classes used (Child1 and Child2) are having the same construction parameters.
class Base
{
public:
Base( int param )
{
}
};
class Child1 : public Base
{
public:
using Base::Base;
};
class Child2 : public Base
{
public:
using Base::Base;
};
std::shared_ptr<Base> getPtr( bool cond, int param )
{
if ( cond )
return std::make_shared<Child1>(param);
else
return std::make_shared<Child2>(param);
}
int main(int argc, char* argv[]) {
bool permissive = ...;
Test::getPtr( permissive, argc );
return 0;
}
Note that permissive is unknown at compilation time.
Is there a way to factorize argument list management in getPtr function with something like:
return std::make_shared<cond?Child1:Child2>(param);
This obviously does not compile but I could not find a way to make something similar work even using templates...
Best solution I could find to avoid duplicating code managing parameters is