473,395 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

deprecating T(V) as c-style cast for POD T in favor of initializationsemantics - Off topic kinda

So I was put straight that double(v) is the same as (double)(v)
recently and I'm somewhat surprised. I'm not the only one.

I think T(V) should have exactly the same semantics as
static_cast<T>(V) for POD T and I'm willing to bet you'll find few C++
developers that would object.

What I'd like to propose is that the c-style cast interpretation of
T(V) for POD T be the same as initialization of a temporary object T
with a value V. i.e. T id(V).

The rationale is that T(V) being a C-ctyle cast is far more dangerous
than using a c-style cast because use of T(V) in templates is often
used to initialize a temporary T and the fact that it becomes a c-
style cast in some cases and is an initialization in others would be
mostly unintended.

I do believe that most current uses of the T(V) syntax for POD T would
be in cases were the developer intended a static cast or
initialization so there would be very few cases where this would be
problematic. Deprecating the c-ctyle cast meaning should have minimal
undesired impact.

I'd like to hear how others feel about this.
Oct 28 '08 #1
3 1756
In article
<84**********************************@e2g2000hsh.g ooglegroups.com>, Gianni
Mariani <ow*******@gmail.comwrote:
So I was put straight that double(v) is the same as (double)(v)
recently and I'm somewhat surprised. I'm not the only one.

I think T(V) should have exactly the same semantics as
static_cast<T>(V) for POD T and I'm willing to bet you'll find few C++
developers that would object.

What I'd like to propose is that the c-style cast interpretation of
T(V) for POD T be the same as initialization of a temporary object T
with a value V. i.e. T id(V).

The rationale is that T(V) being a C-ctyle cast is far more dangerous
than using a c-style cast because use of T(V) in templates is often
used to initialize a temporary T and the fact that it becomes a c-
style cast in some cases and is an initialization in others would be
mostly unintended.

I do believe that most current uses of the T(V) syntax for POD T would
be in cases were the developer intended a static cast or
initialization so there would be very few cases where this would be
problematic. Deprecating the c-ctyle cast meaning should have minimal
undesired impact.

I'd like to hear how others feel about this.
Agreed fully!

A better example is int(expr), where expr being a pointer type causes this
to be a reinterpret_cast. For example, the following code might have been
written when d is a double,

// d is a double
int i = int(d);

but later for some reason d becomes a pointer to double. At that point, it
silently becomes reinterpret_cast<int(d).

However, even if the language can't be changed, compilers could add a
warning when a functional-style cast would result in a const_cast or
reinterpret_cast. Then it becomes a quality-of-implementation issue.
Someone told me that Stroustrup had lamented that this slipped through,
and should have meant static_cast or similar all along.
Oct 28 '08 #2
Gianni Mariani wrote:
So I was put straight that double(v) is the same as (double)(v)
recently and I'm somewhat surprised. I'm not the only one.

I think T(V) should have exactly the same semantics as
static_cast<T>(V) for POD T and I'm willing to bet you'll find few C++
developers that would object.

What I'd like to propose is that the c-style cast interpretation of
T(V) for POD T be the same as initialization of a temporary object T
with a value V. i.e. T id(V).

The rationale is that T(V) being a C-ctyle cast is far more dangerous
than using a c-style cast because use of T(V) in templates is often
used to initialize a temporary T and the fact that it becomes a c-
style cast in some cases and is an initialization in others would be
mostly unintended.
Well it certainly isn't a C style cast for pointers. You can't write

void* p;

int* n(p);

--
Ian Collins
Oct 28 '08 #3
In article <6m*************@mid.individual.net>, Ian Collins
<ia******@hotmail.comwrote:
Gianni Mariani wrote:
So I was put straight that double(v) is the same as (double)(v)
recently and I'm somewhat surprised. I'm not the only one.

I think T(V) should have exactly the same semantics as
static_cast<T>(V) for POD T and I'm willing to bet you'll find few C++
developers that would object.

What I'd like to propose is that the c-style cast interpretation of
T(V) for POD T be the same as initialization of a temporary object T
with a value V. i.e. T id(V).

The rationale is that T(V) being a C-ctyle cast is far more dangerous
than using a c-style cast because use of T(V) in templates is often
used to initialize a temporary T and the fact that it becomes a c-
style cast in some cases and is an initialization in others would be
mostly unintended.

Well it certainly isn't a C style cast for pointers. You can't write

void* p;

int* n(p);
But you can write (and have compile)

typedef int* ip;
void* p;
int* n = ip(p); // static_cast<int*(p)

as well as

typedef int* ip;
int i;
int* n2 = ip(i); // reinterpret_cast<int*(i)

which makes it just as unsafe as a C-style cast. The proposal is to make
these equivalent to

int* n( p ); // error
int* n2( i ); // error

or perhaps

int* n = static_cast<int*(p); // OK
int* n2 = static_cast<int*(i); // error

(I prefer the former, since a static_cast should be verbose and visible)
Oct 28 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Ellinghaus, Lance | last post by:
> > Other surprises: Deprecating reload() >Reload doesn't work the way most people think >it does: if you've got any references to the old module, >they stay around. They aren't replaced. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.