Ok, I see your point now. You are saying that the C++ standard library
can customize itself according to the hardware (while compiled or
installed or loaded into a process). An interesting idea, but seems very
tricky in practice. Do you have any links to any C++ library
implementations actually doing this?
I did not meant something absurdly clever like that. My vision had
similarity with checked iterators that have been ages in gcc and msvc.
Toggled by macro. No binary compatibility. If you want to see how
something really optimizes itself to platform then Qt and boost sources
are nice reading material.
C++ standard library is mostly free from those concerns since it is part
of implementation and implementation targets particular platform. It comes
with compiler and may contain whatever platform-specific voodoo as long
it fulfills the interface requirements given by standard.
I think that standard does not even explicitly say that somewhere must
actually exist text file named "string". We should write "#include <string>"
to use 'std::basic_string said:
The problem is that the string header is typically header-only, which
means it is compiled into application code. In libstdc++, the atomic
operations on refcounter are directly in the header file. On the other
hand, this issue affects ABI, so all code loaded in a process must agree
whether it uses CoW or not. But not all software is compiled directly on
the target machine, not even in Linux, it is common to have some shared
libraries compiled elsewhere. In particular, an application compiled on
another Linux box must work together with libstdc++ installed on the
target machine. IOW, deciding such issues at compile-time is far too
early. And we do not need a new ABI-breaking compiler switch, especially
for something so mundane that it should not deserve any programmer
attention at all as you rightly notice. Note that things like -arch are
easier because they don't affect ABI.
Yes, libstdc++ is very nice effort, it tries to implement portable
standard library for free in C++. Issues of developing portable standard
library are orthogonal to question if there may be standard library on
some platform that implements CoW strings.
Note that some parts of C++11 standard library can not be implemented in
plain C++ with no support from platform or compiler (like <thread> or
<atomic>) anyway. Some other parts can be written (like <string>) but
that does not mean those must be implemented in C++.
I understand term "ABI" as "application binary interface" that is
interface between application and operating system or application and
other application. Unfortunately C++ does not define anything of it;
it does not even have modules yet. Do not you see we have nothing?
Our API with environment is: parameters of 'main', 'cin', 'cout',
'cerr', 'clog', 'system("pause")' and return value of 'main'. ;-(
About cross-compiling I did not understand your point. We *always* do it
for embedded systems. However we use compiler for target platform *and*
library for target platform together. How else?
One could use a non-inlined function or a global static to make this
decision on run-time. But non-inlined functions and global statics are
again known performance hazards. Besides, if libstdc++ introduced such a
feature, it would break ABI compatibility with previous libstdc++
versions.
These are again concerns with what C++ does not deal at all.
Even that
name mangling (to allow linkage with other things) is nowhere mentioned.
So if you have two compilers that compile std::string that can be somehow
exchanged between the modules then that is achieved with efforts outside
of C++. Please do not say that lack of CoW in string somehow helps us out
of that hole here because it does not.
FWIW, LLVM seems to have given up CoW strings. From
http://libcxx.llvm.org/ : "For example, it is generally accepted that
building std::string using the "short string optimization" instead of
using Copy On Write (COW) is a superior approach for multicore machines
(particularly in C++11, which has rvalue references). Breaking ABI
compatibility with old versions of the library was determined to be
critical to achieving the performance goals of libc++."
Yes, LLVM is again great effort. Since most money in it comes from
pockets of Apple it goes where Apple wants it to go. Apple wants it
to work well with its legacy Objective-C libraries and so it has to
work. It should not affect everything else if string in C++ compilers
for OS-X/iOS platforms have CoW or short string or neither. It should
be concern of Apple to measure and to decide.
The C++ standard library is supposed to provide a good general purpose
implementation of all its features. There are always specific corner
cases where it does not work, there is no silver bullet for everything.
It seems however that massive multithreading will become the new norm,
not a corner case. And a CoW string class looks exactly like an extra
custom library for a specific usage case (large strings, copied often),
probably needing extra care in thread passing.
Standard library must be present with implementation and work with the
implementation. It should deal with platform-specific problems internally
in itself. It should not delegate those back to developers.