A selection pattern for C++11 In simulating physical equations (such as fluid dynamics) it is common to write an application that deals with 1, 2, and 3 dimensions. A common C/Fortran approach is to make all vectors (for example) have a size of 3, and then to carry around a global constant to say how many elements are valid. 

We have tried to use C++11 templating to serve the same purpose and the majority of our application is now templated by int DIM. 

There are times that the difference between API for different dimensions requires entirely new classes. For this, we have come up with the following selector pattern -- it seems to be working well, but I was wondering if we have reinvented the wheel and missed something along the way?

In order to construct the pattern, we need several files:

ClassName_fwd.h:

This allows the user to reference the specific implementation of the dimension specific classes in a way that is consistent for all dimensions.

// forward decs
class ClassName_1;
class ClassName_2;
class ClassName_3;
template&lt;int DIM&gt;
struct ClassNameSelector { };
// 1D
template&lt;&gt;
struct ClassNameSelector&lt;1&gt; {
using ClassName = ClassName_1;
};
// 2D
template&lt;&gt;
struct ClassNameSelector&lt;2&gt; {
using ClassName = ClassName_2;
};
// 3D
template&lt;&gt;
struct ClassNameSelector&lt;3&gt; {
using ClassName = ClassName_3;
};
// typedef
template&lt;int DIM&gt;
using ClassName = typename ClassNameSelector&lt;DIM&gt;::ClassName;


ClassName.h

#include &lt;path/to/ClassName_fwd.h&gt;
#include &lt;path/to/ClassName_1.h&gt;
#include &lt;path/to/ClassName_2.h&gt;
#include &lt;path/to/ClassName_3.h&gt;


ClassName.cpp

// explicit instantiations
template struct ClassNameSelector&lt;1&gt;;
template struct ClassNameSelector&lt;2&gt;;
template struct ClassNameSelector&lt;3&gt;;


ClassName_N.h/cpp [optional]

Holds all of the functionality that can be implemented without care for dimension, in addition to pure virtual methods that should be overridden by the dimension-specific implementations. This class sets the dimensionless API for ClassName. In the cpp file, there should be explicit instantiations of this class. This class might be excluded to avoid pure virtual function calls.

/**
* All of the functions that have a constant implementation
* for all dimensions
*/
template&lt;int DIM&gt;
class ClassName_N {
    // non-dimensional functions, i.e.
    void somethingDimensionless(
        const std::array&lt;double, DIM&gt;&amp; dblArray
    ) const;
};


ClassName_[1,2,3].h/cpp

Implements the [1,2,3] dimension version of the class. This should implement the pure virtual functions of ClassName_N in addition to any functions specific to the particular dimension. These addition functions can only be called from other dimensionally specific implementations, otherwise the compiler will complain.

/**
* The dimensionally specific API's
*/
class ClassName_1 : public ClassName_N&lt;1&gt; {
    // dimensional functions, i.e.
    void somethingIn1D() const;
};


Usage

#include &lt;path/to/ClassName.h&gt;

template&lt;int DIM&gt;
void useAnyDimension(const ClassName&lt;DIM&gt;&amp; c) {
    std::array&lt;double, DIM&gt; arr;
    c.somethingDimensionless(arr);
}

void doSomethingIn1D(const ClassName&lt;1&gt;&amp; c) {
    c.somethingIn1D();
}

int main (int argc, char *argv[]) {
    ClassName&lt;1&gt; oneD;
    ClassName&lt;2&gt; twoD;

    // dimension-less 
    useAnyDimension(oneD);
    oneD.somethingDimensionless({0.1});
    twoD.somethingDimensionless({0.1, 0.5});

    // dimension specific
    doSomethingIn1D(oneD);
    oneD.somethingIn1D();

    // the following would give compile-time errors
    //doSomethingIn1D(twoD);
    //twoD.somethingIn1D();

    return 0;
}
This seems unnecessary complicated. Why not just have one simple class template:

template &lt;int DIM&gt;
class ClassName {
public:
    using array = std::array&lt;double, DIM&gt;;

    void somethingDimensionless(const array&amp; dblArray) const;

    template &lt;int D = DIM,
              typename = std::enable_if_t&lt;(D == 1)&gt;&gt;
    void somethingIn1D() const {
        ...
    }
};


And use it directly:

ClassName&lt;1&gt; oneD;
ClassName&lt;2&gt; twoD;


We use SFINAE instead of a static_assert in case you want to write a type trait to check for the existence of somethingIn1D. With a static_assert, ClassName&lt;2&gt;{}.somethingIn1D() wouldn't compile but overload resultion would still find it.