Limit function/constructor arguments to members of a particular type
<p>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?</p>

<p>So, if I have</p>

<pre><code>template &lt;typename T&gt;
struct Foo {
    Foo(int v)
        : value(v) {}

    int value;
};
</code></pre>

<p>and,</p>

<pre><code>struct Bar {
    static const int valid1 = 6;
    static const int valid2 = 9;
    static const int valid3 = 42;
};
</code></pre>

<p>can I limit the valid values that can be passed to the constructor of <code>Foo&lt;Bar&gt;</code> to <code>Bar::valid1</code> ... <code>Bar::valid3</code>?</p>

<p>With this example, I could use an enum in <code>Bar</code> that has a specific name, specified in <code>Foo</code>'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.</p>

<p>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.</p>

<p>I thought about function pointers, so instead of <code>static const int</code>'s in <code>Bar</code> I would have functions that return the values and the constructor of <code>Foo</code> wold accept a function pointer. However static functions behave just like globals with repect to function pointers so there is no <code>Bar</code> qualification required when calling it in <code>Foo</code> so any global function returning an int could be used. It could be made to work by not using static functions in <code>Bar</code> and maintaining an instance of <code>T [=Bar]</code> in <code>Foo</code> 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.</p>

<p>I wonder if there is a standard way of doing this, or what you would recommend.</p>
