Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > {1,2,3} as argument?

Thread Tools

{1,2,3} as argument?

Gavin Deane
Posts: n/a

terminator wrote:
> I insist that C++ is paranoic;I cant see why one shoud be able to
> write:
> f("123");
> but not:
> f({'1','2','3','\0'});
> this is painful

For a function void f(char*) one should NOT be able to write f("123")
as that involves passing a pointer to const data to a function that
accepts a pointer to non-const data. Unfortunately, for backwards
compatibility with C and all the legacy code out there, this one
specific kind of const abuse was deemed necessary. This introduces an
unfortunate logical inconsistency that you are seeing as paranoia.
There are two options to fix the logic:

1. Make everything consistent by removing the distinction between T*
and const T*. That would be mad.

2. Make everything consistent by removing the specific allowed
conversion from string literal to non-const char*. That would be great,
except for the backwards compatibility and legacy code issue. So the
standard does the next best thing and deprecates that conversion so
that it can be removed if and when it is decided that the backwards
compatibility problem is small enough to live with.

So, this inconsistency is not paranoia, it is just an artifact of C++'s
history. There is no reason that any of the C++ code you write today
need make use of the deprecated feature.

Gavin Deane

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off