Boost logo

Boost :

Subject: Re: [boost] [serialization] Proposal: make 'version' trait (and others)enable_if-friendly
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2012-12-06 12:35:48


On Wed, Dec 5, 2012 at 10:20 PM, Andrey Semashev
<andrey.semashev_at_[hidden]>wrote:

> On Thu, Dec 6, 2012 at 10:03 AM, Robert Ramey <ramey_at_[hidden]> wrote:
> > Gabriel Redner wrote:
> >> I have recently run up against a limitation of the serialization
> >> library in terms of specializing the 'version' trait. Fortunately, I
> >> believe the issue has a simple workaround which I don't believe will
> >> break existing code.
> >>
> >> In general, one can change the version of a serialized class by
> >> specializing the 'version' template class:
> >> ==============================
> >> template <typename T>
> >> struct version
> >> {
> >> BOOST_STATIC_CONSTANT(unsigned int, value = 0);
> >> };
> >> ...
> >> template <>
> >> struct version<MyType>
> >> {
> >> BOOST_STATIC_CONSTANT(unsigned int, value = 1);
> >> };
> >> ==============================
> >>
> >> However, this approach does not work if 'MyType' is of the form (see
> >> [1]): ==============================
> >> template <typename T>
> >> struct Foo
> >> {
> >> struct MyType
> >> {
> >> int data1;
> >> };
> >> };
> >>
> >> template <typename T>
> >> struct version<Foo<T>::MyType> // ERROR
> >> {
> >> BOOST_STATIC_CONSTANT(unsigned int, value = 1);
> >> };
> >> ==============================
> >> It's not possible for the compiler to deduce the enclosing type, so
> >> this doesn't work.
> >
> > Hmm - doesn't the fillowing work? - for each T used
> >
> > template <>
> > struct version<Foo<t>::MyType>{
> > BOOST_STATIC_CONSTANT(unsigned int, value = 1);
> > };
>

Is it possible you won't know all T's at the point one wishes to define
this? But, regardless...

> I understand it might be a little more tedious and not that clever
> > but it's much, much, much less time consuming to understand, documment,
> > explain
> > and support. Your suggestion is clever - but I just don't think it's
> > cost effective.
>

You exaggerate :) Besides...

FWIW, the similar approach (with an additional template parameter for
> SFINAE-based dispatch) is used in Boost.Phoenix, Boost.Proto and
> probably elsewhere and is described here:
>
>
> http://www.boost.org/doc/libs/release/doc/html/proto/appendices.html#boost_proto.appendices.implementation.sfinae
>
> With the right use (as described at the linked page) I expect it to be
> nearly as fast as the regular specialization.
>

...it's more or less an established idiom, at least within Boost.

- Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk