If I have a templated class that takes another class as a parameter and which is constructed from a particular type, is there anyway to limit its range of possible construction values to those that are only members of the parameter class?
So, if I have
template <typename T>
struct Foo {
Foo(int v)
: value(v) {}
int value;
};
and,
struct Bar {
static const int valid1 = 6;
static const int valid2 = 9;
static const int valid3 = 42;
};
can I limit the valid values that can be passed to the constructor of Foo<Bar> to Bar::valid1 ... Bar::valid3?
With this example, I could use an enum in Bar that has a specific name, specified in Foo's constructor which would take members of the enum rather than plain ints, but this has the problem that any value can be cast to the enum, so it is easily broken, and more importantly is not extendible to non-integral types.
A strong typedef would do the trick I think, and I believe there's one in boost but I'm looking for solutions I can implement myself with the standard library if possible.
I thought about function pointers, so instead of static const int's in Bar I would have functions that return the values and the constructor of Foo wold accept a function pointer. However static functions behave just like globals with repect to function pointers so there is no Bar qualification required when calling it in Foo so any global function returning an int could be used. It could be made to work by not using static functions in Bar and maintaining an instance of T [=Bar] in Foo and calling the function pointer against this instance, but this seems a bit messy, functions that naturally should be static are not, and I have an instance of a struct which really should be just a container for constants.
I wonder if there is a standard way of doing this, or what you would recommend.