I got a simple CRTP template class that I've been playing with and I came up to a point that I need to have the same ctor but essentially do different things at the constructor initialization list.
I do have an alternative solution but I was wondering if what I wanted to do could be achieved in a better way.
A strip down version of the class is pasted below. You can ignore the Eigen stuff its just a matrix library.
template<class ImplT, size_t N, bool SquareMatrix = false>
class Foo
{
template<bool Test, typename Then, typename Else>
using conditional_t = typename std::conditional<Test, Then, Else>::type;
// this is defined at compile time so no need to init it
using VecN = Eigen::Matrix<double, N, 1>;
// this is a dynamic matrix so dimensions need to be specified at runtime
using MatN = Eigen::MatrixXd;
using MatrixType = conditional_t<SquarePreCovariance, MatN, VecN>;
inline ImplT& impl() noexcept { return static_cast<ImplT&>(*this); }
inline const ImplT& impl() const noexcept { return static_cast<const ImplT&>(*this); }
public:
// if SquareMatrix == false
Foo()
: parameters3(Matrix(N, N))
{}
// if SquareMatrix == true
Foo()
: parameters2(Matrix(N, N)),
parameters3(Matrix(N, N)),
{}
// easy solution that covers all cases
Foo()
: parameters3(Matrix(N, N)),
{
if (SquareMatrix)
parameters2.resize(N,N);
}
private:
VecN parameters;
MatrixType parameters2;
MatN parameters3;
};