Boost logo

Boost :

From: Kevlin Henney (Kevlin.Henney_at_[hidden])
Date: 2000-02-08 06:33:58


Gavin Collings wrote:
>There is a good reason for its existance. As I remember, it was based
>on an example in C++PL (3rd Ed) whereby it is sometimes useful to
>explicitly evoke an implicit type conversion from the compiler, for
>example to direct template argument deduction. I would contend that
>explicit_cast<>() would be a better name for that.

An explicit_cast could be added to the library, but not in the form you
suggest. I have seen it used to express a keyword cast version of the
traditional or function style cast form, ie

     p = (T *) q;

becomes

     p = explicit_cast<T *>(q);

This is appropriate if the power of the raw cast form is desired (and
associated responsibility accepted!) and for some reason the other casts
cannot perform the exact same role. I can't remember where I saw this
first, but I have since used it in a couple of articles.

The implementation would be

     template<typename Target, typename Source>
     Target inline explicit_cast(Source source)
     {
          return Target(source);
     }

which is quite different in both intent and effect of implicit_cast! I must
confess that I am not a great fan of implicit_cast. I have yet to find a
practical use for it, as the other casts (builtin or otherwise) adequately
cover what I need. I would not say that the case for explicit_cast was
incredibly strong, but I would consider it stronger than the one for
implicit_cast.

Kevlin


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