Boost logo

Boost :

From: David Bergman (David.Bergman_at_[hidden])
Date: 2006-07-26 01:45:12

I wrote:

> What I *would have* considered, though, is to have two templates:
> template<...> struct pod_endian { ... no constructor
> but all the endian logic ... }
> and
> template<...> struct endian : pod_endian { ...
> constructors and not much else ... }
> And have conversion operator to get an endian from a pod_endian.

... or have one template with an extra flag, for "create constructor" (much
like a policy...) and use a template specialization

        // Here, 'pod_endian' contains the actual logic, but can be
        // hidden (put in anonymous namespace...)

        template<endianness E, typename RawT, std::size_t n_bytes, alignment
A, bool createConstructor = true>
        struct endian : pod_endian<E, RawT, n_bytes, A> {
                endian(RawT num) : m_value(num) {}
        template<..., typename RawT>
        struct endian<..., RawT, false> : pod_endian<E, RawT, n_bytes, A> {
                // no constructor, and actually nothing else either :-)

This way, we can use either one:

        union MakingMrDribinHappy {
                endian<..., false> mySpecificInt;
                int somethingElse;

        template<typename T>
        void makingMrDawesHappy(T t) { .... }
        makingMrDawesHappy(endian<..., true>(42));

One would of course have to either create named instantiations for both
these kinds or parameterize them, as

        template<bool useConstructor>
        struct big4_t : endian<big, int_least32_t, 4, useConstructor> {};

The use is not as nice:

        big4_t<> myTemplateFriendlyInt;
        big4_t<false> myUnionFriendlyInt;

Ugh :-(


Boost list run by bdawes at, gregod at, cpdaniel at, john at