In C++0x you have template typedef
finally available!
Disclaimer: nothing has been compiled...
From Wikipedia's article:
template< typename second>
using TypedefName = SomeType<OtherType, second, 5>;
which in your case would yield
template <class Type>
using vector3 = vector<Type, 3>;
I can't tell you how much I craved for this ;)
However it doesn't solve the parameters issue. As mentioned, you could try and use variadic templates here, however I am unsure as to their application in this case. The normal use is with recursive methods and you would need to throw a static_assert
in the midst.
Edited to take the comments into account.
template <class Type, size_t Size>
class vector
{
public:
template <class... Args>
vector(Args... args): data({args...})
{
// Necessary only if you wish to ensure that the exact number of args
// is passed, otherwise there could be less than requested
BOOST_MPL_ASSERT_RELATION(sizeof...(Args), ==, Size);
}
private:
T data[Size];
};
Another possibility that is already available is to combine Preprocessor generation with boost::enable_if
.
template <class Type, size_t Size>
class vector
{
public:
vector(Type a0, typename boost::enable_if_c< Size == 1 >::type* = 0);
vector(Type a0, Type a1, typename boost::enable_if_c< Size == 2 >::type* = 0);
// ...
};
Using Boost.Preprocessor for the generation makes this easier.
BOOST_PP_REPEAT(MAX_COUNT, CONSTRUCTOR_MACRO, ~);
// where MAX_COUNT is defined to the maximum size you wish
// and CONSTRUCTOR_MACRO actually generates the constructor
#define CONSTRUCTOR_MACRO(z, n, data) \
vector( \
BOOST_PP_ENUM_PARAMS(n, Type a), \
typename boost::enable_if_c< Size == n >::type* = 0 \
);
The implementation of the constructor is left as an exercise for the reader. It's another call to BOOST_PP_REPEAT
.
As you can see, it soon gets ugly, so you'll be better off if you can use the variadic template version.