以下是我正在处理的代码。
class HoldThis
{
public:
template<typename T> explicit
HoldThis(const T& in)
:a_in(in)
,a_givemebase([=]()-> const SomeBaseType& {
return boost::any_cast<const T&>(a_in); })
{ }
const SomeBaseType& getBase() const
{
return a_givemebase();
}
private:
boost::any a_in;
std::function<const SomeBaseType& ()> a_givemebase;
};
问题是来自T
的类型始终来自SomeBaseType
。
我希望通过某种形式的模板元编程来保留T
及其附带的值in
。
这可能吗?保留T,然后在需要时通过getter返回其值。我使用的是C ++ 11,我不能使用任何C ++ 14或更高版本的功能。
我尝试使用boost::mpl::identity
来获取类型T,但无论如何,我仍然不知道T或其中的值。