3 ms·
>In other words type casting can be written as (foo)2 or as foo(2). Just to make it harder to understand what is going on. Or to make it easy to describe high-
by john_moscow 6y ago
>In other words type casting can be written as (foo)2 or as foo(2). Just to make it harder to understand what is going on.
Or to make it easy to describe high-level behavior where you don't care about the details. In your example, foo and bar could be some complex vector or matrix types with custom allocation/deallocation logic.
So this line:
int x = foo(2) + bar(3);
Is an equivalent of 7 lines in Plain C:
foo tmp_foo;
bar tmp_bar;
construct_foo_from_int(&tmp_foo, 2);
construct_bar_from_int(&tmp_bar, 3);
int x = add_foo_and_bar(&tmp_foo, &tmp_bar);
free_foo(&tmp_foo);
free_bar(&tmp_bar);
Sure, if you don't know what foo and bar are, it's confusing. But if you want to decompose what you are doing from how you are doing it, this syntax rocks.
BTW, (foo)2 syntax is preserved for backward compatibility with C and is deprecated in favor of static/dynamic/reinterpret_cast<>() (that many people don't use due to clunkiness) and foo(2) is unavoidable if foo's constructor has more than 1 argument.