在开发过程中,在写一些回调函数的过程中,常常希望参数是不确定、可变的,这个时候我就希望能有一个参数包,各种参数都可以塞到参数包里面,然后将该参数包作为一个参数。
之前一直使用tuple或结构体指针来实现该功能,但这两者在使用过程中还是有些不方便,于是就想自己写一个参数包的类。
在boost库中有一个any类型,可作为参数包中参数的载体,可自行从boost中摘抄过来。我这边也提供一份代码:
#ifndef class_any
#define class_any
#include <typeinfo>
class any
{
public:
any()
: m_content(nullptr)
{}
//构造过程中,将传入参数复制给holder模板类
template<typename ValueType>
any(const ValueType &value)
: m_content(new holder<ValueType>(value))
{}
any(const any &other)
: m_content(other.m_content ? other.m_content->clone() : 0)
{}
~any()
{
delete m_content;
}
any &swap(any &rhs)
{
std::swap(m_content, rhs.m_content);
return *this;
}
template<typename ValueType>
any &operator=(const ValueType &rhs)
{
any(rhs).swap(*this);
return *this;
}
any &operator=(const any &rhs)