C ++静态操作符重载 [英] C++ static operator overloading
本文介绍了C ++静态操作符重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有可能在静态上下文中重载C ++类运算符吗?例如
Is it possible to overload C++ class operators in the static context? e.g.
class Class_1{ ... }
int main()
{
Class_1[val]...
}
推荐答案
如果您正在使用内置运算符寻找元编程:这样的事情是不可能的 - 内置的运算符操作运行时值,而不是编译时间值。
If you are looking for metaprogramming using the built-in operator: Such a thing isn't possible - the built-in operators operate on runtime values, not on compile time values.
您可以使用 boost :: mpl
,而不是使用内置的运算符,使用它的模板,如
,
加上 > op +
等。
int main() {
using boost::mpl::vector;
using boost::mpl::at_c;
using boost::mpl::plus;
using boost::mpl::int_;
typedef vector<int, bool, char, float> Class_1;
typedef vector< int_<1>, int_<2> > Numeric_1;
at_c<Class_1, 0>::type six = 6;
typedef plus<at_c<Numeric_1, 0>::type
,at_c<Numeric_1, 1>::type>::type r;
int i3[r::value] = { 4, 5, 6 };
return ((i3[0] + i3[1] + i3[2]) * six) == 90;
}
这篇关于C ++静态操作符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文