Boost logo

Boost :

From: Jeffrey C. Jacobs (darklord_at_[hidden])
Date: 2002-09-11 08:48:15

Thanks Herve!

That was a good read and very helpful in my thoughts. Wow though, if the
answer in the MLP I do slightly cringe.

In my implementation I should first preface that I made a slight mistake in
my reading of the standard. In other words, the UQ (Unanswered Question) #
1 for the [un]signed_cast methods should actually read:

// 1. Cannot use on immediates and having problems with variables
// too; likely compiler's fault. In MSVC 6.0 this translates to
// C2667, C2668; see MSDN Knowledge Base article Q240869 for
// details.

And noted that:

// cv-qualification of pass-by-value is not overloaded;
// See ISO Standard, Section 8.3.5(3)

So have modified my code accordingly. That means that pass-by-reference has
to support const and volatile forms but the copy-constructed forms (pbv)
have to be one of the 3 cv-forms, likely the const form since const should
be preserved and the pbv form can't be used as an l-value anyway (can't
assign to it) so it seems the most logical. That having been said, the
current implementation for [un]signed_cast has:

Integral _T
Integral &_T
const Integral &_T
volatile Integral &_T

I still have a problem with immediates since ideally I want to treat all
operations as "reinterpret_casts" in the sense that I want to avoid
unnecessary temporaries. As noted though, since there is a bug in MSVC 6.0
(I think this is fixed in 7.0 / .NET) I can only support one of the above 4
safely on MSVC.

I do escape the partial-specialization monster though since all my templates
are single-parameter and therefore only support complete specialization

Anyway, WRT the very interesting thread on Boost.(type|integer)_traits
missing feature, thanks for the heads up. Seems John Maddock is considering
a bunch of changes to integer_traits.hpp so your thoughts may already be in
the works for Boost 1.29 making mine moot. Not that I mind, I'm just
thinking standardization of function is good; I don't care about the names
or who does it, but I'm happy to write it if no-one else wants. I -- and it
seems you Herve -- would certainly find it useful. (Must remember to check
the latest CVS to see if John hasn't already implemented something similar!)

Paul Mensonides' MPL approach OTOH was quite a lot to swallow -- so much so
I am still digesting! So I'll reserve comment on that for another day. One
think I like about my approach is that it has very little run-time overhead:

The sign_traits class is simply 3 typedefs which have NO runtime effect on
size or space (they're just aliases). That having been said, there may be a
cost simply in the existence of a class definition but again a class of
typedefs should be optimized out ideally (otherwise many other elements of
boost can be very expensive!).

The pbv [un]signed_cast has as many as 3 temporaries that should hopefully
be optimized out through inlining and since the static_cast is for like
integral types hopefully it will not "reallocate". Further, because the
template parameter IS the type passed in, unless you override that type
explicitly (thus potentially introducing a further temporary in the
potentially implicit cast to the template type), there should be no other
implicit casting of the value: either it is specialized and compiles or **TO
DO** it would fail to compile if the type did not support the concept of

The pbr [un]signed_cast forms should have no temporaries as everything is by
reference and because it's inlined, it should also have no run-time overhead
in time or space (except if used on an immediate).

So anyway, next step (apart from solving the rest of the UQ) is an
investigation of the MPL and a scanning of CVS tree for updates. Anyway,
thanks for your thoughts Herve and hope we at least get SOME
signedness-casting operator, whatever it may be!


"Herve Bronnimann" <hbr_at_[hidden]> wrote in message
> On Tue, Sep 10, 2002 at 04:35:34PM -0400, Jeffrey C. Jacobs wrote:
> > I have written a proposal for the boost community (a small one that will
> > help me with other proposals on which I am working) and was wondering
> > if this does not become a part of boost (though I would like you to
> > it) maybe some of you would be willing to help me answer some of the
> > "Unanswered Questions".
> You may want to look at a short thread dated May 16-18 2002, between
> myself, John Maddock, and Paul Mensonides, where we discussed similar
> problem. Thne thread was entitled:
> Boost.(type|integer)_traits missing feature?
> I had a problem with partial specialization, and Paul suggest some
> metaprogramming. I haven't taking a close look at MPL but it might be
> just what you need. The problem was related to short being the same type
> as int (or char, can't remember) on some platforms, and so a partial
> specialization was be defined twice. With an associative list of types,
> taking the first match, the problem disappeared.
> John Maddock said he would probably add it to type_traits but didn't
> know when he'd have the time.
> > // Template class sign_traits<Integral> --
> > // Supplies:
> > // typedef integer_type -- same type as Integral
> > // typedef signed_type -- the equivalent of "signed Integral"
> > // typedef unsigned_type -- the equivalent of "unsigned Integral"
> > // Template function signed_cast<cv-unqualified Integral>
> > //
> > // Parameters:
> > // cv-qualified Integral _T -- type to be converted
> > // OR
> > // cv-qualified Integral &_T -- reference to type to be converted
> > // Returns:
> > // cv-qualified signed Integral -- if _T is pass by value
> > // OR
> > // cv-qualified signed Integral & -- if _T is pass by reference.
> > // Template function unsigned_cast<cv-unqualified Integral>
> > // Parameters:
> > // cv-qualified Integral _T -- type to be converted
> > // OR
> > // cv-qualified Integral &_T -- reference to type to be converted
> > // Returns:
> > // cv-qualified unsigned Integral -- if _T is pass by value
> > // OR
> > // cv-qualified unsigned Integral & -- if _T is pass by reference.
> These look good. More thorough than what I have, in that they also
> handle the cv-qualification.
> --
> Herve'
> _______________________________________________
> Unsubscribe & other changes:

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