{1,2,3} as argument?

G

Gavin Deane

terminator said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top