Boost序列化:指定模板类版本
我有一个序列化的模板类(称之为 C),我想为其指定一个用于 boost 序列化的版本。 由于 BOOST_CLASS_VERSION 不适用于模板类。 我尝试了这个:
namespace boost {
namespace serialization {
template< typename T, typename U >
struct version< C<T,U> >
{
typedef mpl::int_<1> type;
typedef mpl::integral_c_tag tag;
BOOST_STATIC_CONSTANT(unsigned int, value = version::type::value);
};
}
}
但它无法编译。 在 VC8 下,对 BOOST_CLASS_VERSION 的后续调用会出现以下错误:
错误 C2913:显式专业化; 'boost::serialization::version' 不是类模板的特化
正确的方法是什么?
I have a template class that I serialize (call it C), for which I want to specify a version for boost serialization. As BOOST_CLASS_VERSION does not work for template classes. I tried this:
namespace boost {
namespace serialization {
template< typename T, typename U >
struct version< C<T,U> >
{
typedef mpl::int_<1> type;
typedef mpl::integral_c_tag tag;
BOOST_STATIC_CONSTANT(unsigned int, value = version::type::value);
};
}
}
but it does not compile. Under VC8, a subsequent call to BOOST_CLASS_VERSION gives this error:
error C2913: explicit specialization; 'boost::serialization::version' is not a specialization of a class template
What is the correct way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了避免您的库过早依赖于 Boost.Serialization,您可以转发声明:
而不是包含标头。
要声明类的版本,您可以执行以下操作:
由于它不使用
BOOST_CLASS_VERSION
宏,因此仍然不需要提前包含 Boost.Serialization 标头。(出于某种原因,在 C++14 中,
static const [constexpr] unsigned int value = 16;
对我不起作用)。To avoid premature dependency of your library on Boost.Serialization you can forward declare:
Instead of including the header.
To declare the version of you class you can do:
Since it doesn't use the
BOOST_CLASS_VERSION
macro still doesn't need premature inclusion of the Boost.Serialization headers.(for some reason
static const [constexpr] unsigned int value = 16;
doesn't work for me, in C++14).我能够正确使用宏 BOOST_CLASS_VERSION 直到我将其封装在命名空间中。 返回的编译错误是:
正如之前编辑中所建议的,将 BOOST_CLASS_VERSION 移动到全局范围解决了该问题。 我更喜欢使宏保持接近引用的结构。
I was able to properly use the macro BOOST_CLASS_VERSION until I encapsulated it inside a namespace. Compilation errors returned were:
As suggested in a previous edit, moving BOOST_CLASS_VERSION to global scope solved the issue. I would prefer keeping the macro close to the referenced structure.
:-)
:-)