Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2006-01-30 12:52:49


"David Abrahams" wrote
Andy Little wrote
>> implicit_cast<t>(S) will succeed wherever
>> is_convertible<S,T>::value ==true
>
> I think that would be a lie for some reasonable definitions of
> "succeed":

OK how about

implicit_cast<t>(S) will usually 1 succeed if
boost::is_convertible<S,T>::value
is true, but will always cause a compile_time error if
boost::is_convertible<S,T>::value is false.

------------
[note1]

The following is one situation where implicit_cast wont succeed

        struct foo
        {
           operator int() { throw "fooled ya"; }
        };

FWIW enclosed updated version of <libs/implicit_cast/implicit_cast.qbk>
together
with generated html

regards
Andy Little


    = = = = = = = =
   3D"boost.png

    _________________________________________________________________

   
   
   

   

   
   Copyright © 2005 -2006
   
    = Distributed under the Boost Software License, Version 1.0. = (See
   accompanying file LICENSE_1_0.txt or copy at = [1] http://www.boost.org/LICENSE_1_0.txt = ) =

    * [2]Synopsis * [3]Summary * [4]Example
   
   

   
    = header [5]"boost/implicit_cast.hpp"
    template <typename T> T implicit_cast( T1 x); Requires: T1 is implicitly convertible to T. Returns: An object of type T whose value is the result of implicit conversion of x from type T1 to T.

   
   

   
    = implicit_cast is used when the source type is implicitly convertible to the target type, to force the implicit conversion. = It's
   less liberal than static_cast, which will convert in the opposite direction.
   
    = implicit_cast<t>(S) will usually [6]^1 succeed if
   [7]boost::is_convertible<S,T>::value is true, but = will always cause a
   compile_time error if = [8]boost::is_convertible<S,T>::value is false.
   
   

   
    = As an example application lets take initialisation of a user defined type (UDT) where its usual to require explicit initialisation (or value initialisation) by making the constructor explicit to prevent implicit initialisation. In udtA the explicit constructor means that udtA can be initialised with types T, but only using the explicit or value initialisation syntax.
    #include <boost/implicit_cast.hpp> template <typename T> struct udtA{ typedef T value_type; T val; template <typename T1> explicit udtA( T1 const & t) : val(t){} };

   
    = The problem is that udtA can be value initialised by an unexpectedly wide range of types as shown in the first example below. The initialisation of a udtA<double> object is safe because it can = only be
   value initialised by inbuilt types. The nested = udtA<udtA<double> > allows
   initialisation by its value_type, = a udtA<double>, as shown in the
   initialisation of vA1, but it also = allows initialisation by an int as
   shown in vA2. This may be, but is = probably not, what the author of udtA
   intended.
    udtA<double> vA0(1); udtA<udtA<double> >vA1(vA0); udtA<udtA<double> >vA2(1);

   
    = This problem can be prevented by adding boost::implicit_cast in the
   initialiser for = val as shown in udtB.
    template <typename T> struct udtB{ typedef T value_type; T val; template <typename T1> explicit udtB( T1 const & t) : val(boost::implicit_cast<T>(t)){}

   
    = template <typename T> = struct udtB{ = typedef T value_type; = T
   val; = template <typename T1> = explicit udtB( T1 const & t) : val(boost::implicit_cast<T>(t)){} = };
   
    = Attempting to initialise a udtB by a type not implicitly convertible to
   = its value_type will be prevented, as in the attempted initialisation of vB2 by an int shown below.
    udtB<double> vB0(1); udtB<udtB<double> >vB1(vB0); udtB<udtB<double> >vB2(1); // Error!
   
    = = Note1
    = = The following is one situation where is_convertible<foo,int> will
   = return true but where implicit_cast wont succeed.
    struct foo{ operator int() { throw "fooled ya"; } };

   
   
    = Last revised: January 30, 2006 at 17:47:56 = GMT
    =
    _________________________________________________________________

   
    = =
References

   1. 3D"http://www.boost.org/LICENSE_1_0.txt"
   2. file://localhost/tmp/3D"index.html#synopsis"
   3. file://localhost/tmp/3D"index.html#summary"
   4. file://localhost/tmp/3D"index.html#example"
   5. file://localhost/../boost/implicit_cast.hpp"
   6. file://localhost/tmp/3D"index.html#note1"
   7. file://localhost/../doc/html/boost_typetraits/reference.html#boost_typetr 8. file://localhost/../doc/html/boost_typetraits/reference.html#boost_typetr



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