如何简化boost变体上的加号动作
How to simplify the plus action on boost variant?
我有boost变体类型定义:
typedef boost::variant< bool, int, float, double> VariantType;
我想在它上实现加/减/乘/除动作。以Add类为例。问题是,如果在VariantType中添加新类型,例如std::string,则Add类必须使用新类型更新。
struct Add : public boost::static_visitor<VariantType> {
template <typename T>
T operator() (T a, T b) const {
return a + b;
}
float operator() (int a, float b) const {
return a + b;
}
float operator() (float a, int b) const {
return a + b;
}
double operator() (int a, double b) const {
return a + b;
}
double operator() (double a, int b) const {
return a + b;
}
double operator() (float a, double b) const {
return a + b;
}
double operator() (double a, float b) const {
return a + b;
}
VariantType operator() (bool a, int b) const {
throw std::invalid_argument("bool and int can't Plus");
}
VariantType operator() (bool a, float b) const {
throw std::invalid_argument("bool and float can't Plus");
}
VariantType operator() (bool a, double b) const {
throw std::invalid_argument("bool and double can't Plus");
}
VariantType operator() (int a, bool b) const {
throw std::invalid_argument("int and bool can't Plus");
}
VariantType operator() (float a, bool b) const {
throw std::invalid_argument("float and bool can't Plus");
}
VariantType operator() (double a, bool b) const {
throw std::invalid_argument("double and bool can't Plus");
}
};
用法:
VariantType v1 = 1;
VariantType v2 = 2.1;
VariantType v3 = boost::apply_visitor(Add(), v1, v2);
cout<<boost::get<double>(v3)<<endl; //Print 2.2
GCC版本是4.8.2;boost版本为1.57.0;如何简单地添加类?谢谢。
让它成为多态函子:
Live On Coliru
#include <boost/variant.hpp>
#include <iostream>
using VariantType = boost::variant<int, float, double, bool>;
struct Add : public boost::static_visitor<VariantType> {
template <typename T, typename U>
auto operator() (T a, U b) const -> decltype(a+b) { return a + b; }
template <typename T> VariantType operator()(bool, T) const { throw std::invalid_argument("Can't to bool"); }
template <typename T> VariantType operator()(T, bool) const { throw std::invalid_argument("Can't add bool"); }
VariantType operator()(bool,bool) const { throw std::invalid_argument("Can't add bools"); }
};
int main() {
std::cout << std::boolalpha;
VariantType specimens[] = { int(42), 3.14f, 3.14, true };
for (auto lhs : specimens)
for (auto rhs : specimens)
{
try {
std::cout << lhs << " + " << rhs << " == " << boost::apply_visitor(Add{}, lhs, rhs) << "n";
} catch(std::exception const& e) {
std::cout << lhs << " + " << rhs << " ==> " << e.what() << "n";
}
}
}
打印:
42 + 42 == 84
42 + 3.14 == 45.14
42 + 3.14 == 45.14
42 + true ==> Can't add bool
3.14 + 42 == 45.14
3.14 + 3.14 == 6.28
3.14 + 3.14 == 6.28
3.14 + true ==> Can't add bool
3.14 + 42 == 45.14
3.14 + 3.14 == 6.28
3.14 + 3.14 == 6.28
3.14 + true ==> Can't add bool
true + 42 ==> Can't to bool
true + 3.14 ==> Can't to bool
true + 3.14 ==> Can't to bool
true + true ==> Can't add bools
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 提升精神:解析布尔表达式并简化为规范范式
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- 函数何时会在c++中包含stack_Unwind_Resume调用
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何在boost beast http请求中设置http头
- Boost Spirit,获取迭代器内部语义动作
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- 为什么这种类型的擦除实现(简化的 boost:any)会出现分段错误
- C++11: boost::make_tuple 与 std::make_tuple 有何不同?
- 如何制作boost::变体的简化版本
- 如何简化 boost::static_vistor 的派生类
- 如何简化boost变体上的加号动作