Explicit stating what exactly do you mean makes the program cleaner
and safer.
That's why there are static_, const_ and reinterpret_ casts in C++,
versus some versatile C cast.
On other hand, most common casts look like that:
Foo foo = some_cast<Foo>(bar); // some_ is static_, dynamic_ or
reinteret_
that is, the parameter of the cast operator matches with the target.
So we can introduce helpers:
template<class Src> struct staticcast_t
{
Src* src_;
staticcast_t(Src* src) : src_(src) {}
template<class Dst> operator Dst* () const { return
static_cast<Dst*>(src_); }
};
template<class Src> staticcast_t<Src> staticcast(Src* src) { return
staticcast_t<Src>(src); }
// and alike helpers for other casts, and for references as well as
for pointers.
Foo* foo; Bar* bar;
foo = staticcast(bar); foo = dynamiccast(bar); foo =
reinterpretcast(bar); // reinterpret can be non-template, just
<void> :)
We can even introduce explicit C-style cast (I don't know why, but yes-
we-can) that does static/const/reinterpret cast depending on types.
foo = cstylecast(bar);
On 17 май, 14:40,
snake5crea...@gmail.com wrote:
> I know a lot of die-hard C++ fans are going to be against this, against the
> evil raw pointers and *horrifying **potential *memory problems etc. but I
> feel like this is a good step towards making the language useful for a few
> more situations.
>
> I'd like to see the requirement for extremely verbose explicit casts
> removed, at least depending on a compiler setting.
>
> *Why?*
> I never liked the need to explicitly state unnecessary things - most
> pointer types match in terms of format and memory usage anyway (they're
> just integers, after all). More typing doesn't keep anyone away from
> messing directly with memory. Hasn't kept anyone away. And shouldn't. If
> people are writing code in C++, in my opinion it should be a fully
> functional superset of C, not some bling-bling replica. So basically I want
> to type less to achieve just about the same thing + cleaner code. Casts
> shouldn't take so much space that they practically obfuscate the most
> important bits of information.
> Another reason for it is that you're already moving that way with "auto"
> and lambda functions.
>
> *How I imagine it*
> foo_struct* bar = some_char_ptr;
> quite_a_long_name_struct* baz = some_char_ptr + bar->where_is_it;
>
> *How it currently is*
> foo_struct* bar = (foo_struct*) some_char_ptr;
> quite_a_long_name_struct* baz = (quite_a_long_name_struct*) ( some_char_ptr
> + bar->where_is_it ); // note the second set of parentheses - casts require
> those
> // here's a "nicer" alternative:
> quite_a_long_name_struct* baz = reinterpret_cast< quite_a_long_name_struct*
>
> >( some_char_ptr + bar->where_is_it );
>
> *How haters are going to imagine it*
> #define A foo_struct
> #define B quite_a_long_name_struct
> A* bar = (A*) some_char_ptr;
> B* baz = (B*) ( some_char_ptr + bar->where_is_it );
> #undef A
> #undef B
>
> P.S. If you're going to bore me with random theory like "pointers could be
> unique identifiers uncapable of representing anything but the type" then
> please let me remind you that the second word in "computer science" means
> experiments, putting stuff into practice and watching it, trying to
> understand how it works. I've done my fair share of science and for us,
> low-level programmers, it just doesn't work in such a restricted and
> impractical way.
> P.P.S. I've been informed that accepting such a proposal would mean having
> many pages of the standard rewritten. I strongly urge you to reconsider it
> nonetheless.