
I was wondering whether it makes sense to have another kind of cast in boost, which behaves like the following: template<typename T, typename ...U> T construct_cast(U &&... u) { T t(std::forward<U>(u)...); return t; } This cast works if std::is_constructible<T, U...>::value is true, and fails if it is false. It presents a slightly more explicit form of boost::implicit_cast<T> 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<T>(u). What do you think about it?

Message du 17/04/11 18:36 De : "Johannes Schaub (litb)" A : boost@lists.boost.org 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/h...).
participants (2)
-
Johannes Schaub (litb)
-
Vicente BOTET