Subject: Re: [boost] Remembering that T(a) can cause dangerous conversion like a C-style cast
From: michi7x7 (michi_at_[hidden])
Date: 2011-11-03 13:28:03


Hi,

> An essential question:
> Should boost::lambda::constructor<int>()("") compiles?

Definitely no.

> I have crafted a general replacement of T(a) as a workaround for
> dangerous conversions caused by T(a) which may happen in some kinds of
> templates.

There is already one in boost (Functional/Factory) AFAIK, but it might
has the same problem:
http://www.boost.org/doc/libs/1_47_0/libs/functional/factory/doc/html/index.html

boost::value_factory<T>()(arg1,arg2,arg3)
// same as T(arg1,arg2,arg3)

> To avoid the unexpected conversion, I first proposed use of T{...} .
> But it is not a perfect solution because it changes the meaning in some
> cases like std::vector<int>.
But this is exactly the use-case T{...} was made for. Of course
initializer_lists make this approach completely unusable...

Another possible solution would be to use static_cast<T>(a) for one
constructor-argument, as static_cast is type-safe.

> As shown in the top of this message, I picked boost::lambda::constructor
> as a concrete example, and verified that the problem really exists in
> the wild.

Not if lambda::constructor was well written, but it's hard to take care
of all these pitfalls (typical C++).

> Thinking more, I wonder why such dangerous conversion was explicitly
> injected to T(a) in the first place. I first thought of some
> C-compatibility issues. But now I think it cannot be, because T(a) is a
> new construct in C++. Is it possible to remove this dangerous
> conversion path in a future revision of the standard?

T(a) is a function-style cast which was used in C, removing it would be
rather difficult.

HTH

michi7x7