Is it possible to address the following scenario of choosing the argument at runtime开发者_如何学C with mpl?
struct A {
A(int number) { /* use number */ } };
struct B { };
template <typename T, int n>
struct A_or_B_Holder {
A_or_B_Holder() : _t( /* void or 'n', depends on T */ ) { }
private:
T _t;
};
A_or_B_Holder<B, void> b;
A_or_B_Holder<A, 3> a;
Ideally,
A_or_B_Holder<B> b;
A_or_B_Holder<A, 3> a;
Your first problem is that void
is a type, not an integer. You could make the template accept two types, the second being either boost::mpl::int_
or void
.
Then you could either specialize the entire struct, or you could put the data member in a base class and specialize that.
#include <boost/mpl/int.hpp>
struct A {
A(int number) { /* use number */ } };
struct B { };
template <class T, class Value>
struct A_or_B_Holder_Base{
A_or_B_Holder_Base(): _t(Value::value) {}
protected:
T _t;
};
template <class T>
struct A_or_B_Holder_Base<T, void> {
A_or_B_Holder_Base(): _t() {}
protected:
T _t;
};
template <typename T, typename Value>
struct A_or_B_Holder : public A_or_B_Holder_Base<T, Value> {
};
using boost::mpl::int_;
A_or_B_Holder<A, int_<3> > x;
A_or_B_Holder<B, void> y;
A_or_B_Holder<A, void > w; //error, no default constructor
A_or_B_Holder<B, int_<3> > z; //error, no int constructor
More natural might be not to require the parameter to be a compile-time constant (as you are turning the compile-time constant into a run-time variable anyway). Just overload the constructor.
struct A {
A(int number) { /* use number */ } };
struct B { };
template <typename T>
struct A_or_B_Holder {
A_or_B_Holder() : _t( ) { }
A_or_B_Holder(int number): _t(number) {}
private:
T _t;
};
A_or_B_Holder<B> b;
A_or_B_Holder<A> a(3);
You could use a Boost.Variant
#include <boost/variant.hpp>
struct A
{
};
struct B
{
B(int)
{
}
};
int main()
{
boost::variant<A, B> val;
if (/*some condition*/)
{
val = A();
}
else
{
val = B(5);
}
}
If you are more interested in how this is implemented, rather than what you can use, I suggest you check out the implementations of Boost.Any
and Boost.Variant
.
I think that passing different arguments at run time is impossible. You'll have to have a standard constructor that all T types will have. You can make all T types construct from std::vector<void*>
and use that to pass a variable number of variable type arguments, but that's super dangerous.
However, your sample seems to actually be deciding at compile-time, not run-time, which is solvable.
const int NO_INT_PARAM=INT_MIN;
template <typename T, int n>
struct A_or_B_Base {
A_or_B_Holder() : _t(n) { }
private:
T _t;
};
template <typename T>
struct A_or_B_Base<T, NO_INT_PARAM> {
A_or_B_Holder() : _t() { }
private:
T _t;
};
template <typename T, int n=NO_INT_PARAM>
struct A_or_B_Holder : A_or_B_Base<T, n>{
A_or_B_Holder() : A_or_B_Base() { }
/* other functions*/
};
A_or_B_Holder<A, 3> a; //uses A(3) constructor
A_or_B_Holder<B> b; //uses B() constructor
A_or_B_Holder<A> c; //compiler error! A() doesn't exist!
A_or_B_Holder<B, 5> d; //compiler error! B(5) doesn't exist!
A_or_B_Holder<B, NO_INT_PARAM> e; //same as b, uses B() constructor
精彩评论