Boost logo

Boost :

Subject: Re: [boost] Would a construct_cast<T>(u..) be useful?
From: Vicente BOTET (vicente.botet_at_[hidden])
Date: 2011-04-17 14:06:04


> Message du 17/04/11 18:36
> De : "Johannes Schaub (litb)"
> A : boost_at_[hidden]
> Copie à :
> Objet : [boost] Would a construct_cast(u..) be useful?
>
> I was wondering whether it makes sense to have another kind of cast in
> boost, which behaves like the following:
>
> template
> T construct_cast(U &&... u) {
> T t(std::forward(u)...);
> return t;
> }
>
> This cast works if std::is_constructible::value is true, and fails
> if it is false.
>
> It presents a slightly more explicit form of boost::implicit_cast in that
> explicit constructors of T are allowed, and for the case of a single-
> elemented parameter pack, explicit conversion functions of U converting to
> "T" are allowed. It does not allow non-implicit conversions like void* ->
> nonvoid*, Base* -> Derived*.
>
> Advantages over not having construct_cast:
>
> - Converting expressions to "bool" by use of "contextually converted to
> bool", without using things like "!!x" or "x ? true : false" (the first may
> trigger user defined opeators for "operator!", which may not be desired).
> - No need for introducing a temporary named variable like the above "t".
> - If all you want is to consider contexual conversions, explicit
> constructors and conversion functions, this can be used. All these
> conversion seem "safe", compared to the set of conversions allowed by T(u)
> and even static_cast(u).
>
> What do you think about it?

Hi,

happy to see that someone else has this need. The library Boost.Conversion [1] on the review schedule defines a convert_to function that behaves like your construct_cast. In addition the library defines a framework that allows the user to convert types that are unrelated.

I will really appreciate if you can take a look and tell me if you find there what you are locking for or if you want something else.

Best,
Vicente

Boost.Conversion ([1]https://svn.boost.org/svn/boost/sandbox/conversion/libs/conversion_ext/doc/html/index.html).


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