使用boost::type_erasure模块进行类型擦除并实现convert功能
在C++中,有时候需要将类型擦除,使得不同的类型可以被当做同一种类型使用。boost::type_erasure模块提供了类型擦除的解决方案,并且可以方便地实现convert功能。下面是一个简单的使用boost::type_erasure模块的convert测试程序:
#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/builtin.hpp>
#include <boost/type_erasure/operators.hpp>
#include <iostream>
using namespace boost::type_erasure;
// 定义可操作符类型的Concept
template<typename T>
struct can_add
{
template<typename U>
static auto test(U *p) -> decltype(std::declval<U>() + std::declval<T>(), std::true_type());
template<typename>
static auto test(...) -> std::false_type;
static constexpr bool value = decltype(test<T>(nullptr))::value;