1

Almost a rehash of What's the difference between function(myVar) and (function)myVar?

But I want to know:

What is the name of these variants and are they 'bad'?

type(myVar) is constructor like syntax, but for a basic type is it the same as doing a C-style cast which is considered bad in C++?

(type)myVar this one certainly does seem to be a C-style cast and thus must be bad practice?

I've seen some instances where people replace things like (int)a with int(a) citing that the C-style version is bad/wrong yet the linked question says they're both the same!

Community
  • 1
  • 1
paulm
  • 5,629
  • 7
  • 47
  • 70
  • What do you mean by `function`? `int` isn't a function. Also, what are you asking for the name of? The returned object? The syntax? – user2357112 Feb 17 '14 at 23:41
  • The syntax, I guess function should really be type, I'll update the question – paulm Feb 17 '14 at 23:42

1 Answers1

3

What is the name of these variants

  • type(expr) is known as a function-style cast.
  • (type)(expr) is known as a C-style cast.

and are they 'bad'?

Yes. First off, both are semantically completely equivalent. They are “bad” because they aren’t safe – they might be equivalent to a static_cast, but equally a reinterpret_cast, and without knowing both type and the type of expr it’s impossible to say1. They also disregard access specifiers in some cases (C-style casts allow casting inside a private inheritance hierarchy). Furthermore, they are not as verbose as the explicit C++ style casts, which is a bad thing since casts are usually meant to stand out in C++.


1 Consider int(x): Depending on x’ type, this is either a static_cast (e.g. auto x = 4.2f;) or a reinterpret_cast (e.g. auto x = nullptr; on an architecture where int is large enough to hold a pointer).

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Does this mean function-style cast is a C feature? – paulm Feb 17 '14 at 23:50
  • Do you say that doing for a `unsigned char c;` `int(c)` would resemble the same effects as of a `reinterpret_cast(c)`? – πάντα ῥεῖ Feb 17 '14 at 23:51
  • @paulm No, definitely not c. – πάντα ῥεῖ Feb 17 '14 at 23:51
  • 2
    @πάνταῥεῖ No. I’m saying that for an (unknown) type `T`, the syntatically same cast might be either a `static_cast` or a `reinterpret_cast`. Making the cast explicit clarifies this. People are divided about whether they deem `int(c)` etc. acceptable. Personally, I don’t. If you want to use constructor-like syntax, use `int{c}` (but this only works where an implicit conversion is allowed) or `static_cast(c)`, and avoid casting wherever possible. Where casts are too verbose, I wrap them in a function. – Konrad Rudolph Feb 17 '14 at 23:52
  • 1
    This answers my comment: http://stackoverflow.com/questions/4474933/what-exactly-is-or-was-the-purpose-of-c-function-style-casts how do you know if it would become a static or a reinterpret cast though? – paulm Feb 17 '14 at 23:54
  • 2
    @paulm In general, only by knowing all involved types and by knowing which casts are legal on these types. Consult the standard for the rules dictating between which types a `static_cast` and a `reinterpret_cast` is defined. The rules are unfortunately far from straightforward. All the more reason to avoid unclear casts which muddy the waters further. – Konrad Rudolph Feb 18 '14 at 00:02
  • It can also be a const_cast, might be worth mentioning. – user541686 Feb 18 '14 at 00:13
  • My takeaway from this is never use it unless you have to (in templates) – paulm Feb 18 '14 at 01:11
  • @paulm Actually, you don’t have to use it even in templates (at least I haven’t come across a legitimate usage yet). – Konrad Rudolph Feb 18 '14 at 08:00