The code shown is valid (all C++ Standard versions, I believe). The similar restrictions are all listed in [reserved.names]. Since read
is not declared in the C++ standard library, nor in the C standard library, nor in older versions of the standard libraries, and is not otherwise listed there, it's fair game as a name in the global namespace.
So is it an implementation defect that it won't link with -static
? (Not a "compiler bug" - the compiler piece of the toolchain is fine, and there's nothing forbidding a warning on valid code.) It does at least work with default settings (though because of how the GNU linker doesn't mind duplicated symbols in an unused object of a dynamic library), and one could argue that's all that's needed for Standard compliance.
We also have at [intro.compliance]/8
A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any well-formed program. Implementations are required to diagnose programs that use such extensions that are ill-formed according to this International Standard. Having done so, however, they can compile and execute such programs.
We can consider POSIX functions such an extension. This is intentionally vague on when or how such extensions are enabled. The g++ driver of the GCC toolset links a number of libraries by default, and we can consider that as adding not only the availability of non-standard #include
headers but also adding additional translation units to the program. In theory, different arguments to the g++ driver might make it work without the underlying link step using libc.so
. But good luck - one could argue it's a problem that there's no simple way to link only names from the C++ and C standard libraries without including other unreserved names.
(Does not altering a well-formed program even mean that an implementation extension can't use non-reserved names for the additional libraries? I hope not, but I could see a strict reading implying that.)
So I haven't claimed a definitive answer to the question, but the practical situation is unlikely to change, and a Standard Defect Report would in my opinion be more nit-picking than a useful clarification.
sync_with_stdio(0)
is needed or not depends a lot on the contest. For example, lots of local Russian ICPC contests have very tight time limits and you'd better not use<iostream>
at all because it's slow. Obviously, it's a combination of large I/O and specific compilers/default compilation flags used on such competitions. – Lilasfread
is in the standard, so I would be less surprised, I was thinking aboutread(2)
, will clarify. – Lilasread
normalloc
are reserved identifiers according to the standard. Of course, in some contexts (e.g. where<stdlib.h>
is included, which declaresmalloc()
) having a variable namedmalloc
would be problematical, just as it would be problematical in some contexts having both a user-declared function namedfoo()
and a variable namedfoo
. – Carabiniere#include <unistd.h>
do you get a compiler error for redefinition ofread
? – Smedleyposix
(at the time I write this...). POSIX reserves some identifiers regardless of header inclusion in 2.2.2 The Name Space: "... (skip a lot) ... The following identifiers are reserved regardless of the inclusion of headers: ... malloc ..." Interestingly,read
is not on that list. – Bambibambiemalloc
is a function from C standard library which is included in C++ standard library. And in C++ an include file is allowed to load symbols from other include files so a cautious programmer should never use a symbol defined anywhere in the standard library. But according to the standardsread
is not one of them... – Bertharead
asextern "C"
, as I would expect (and link statically). – Viscoidmain
(or other entry point) in the global namespace. There is nothing I can do wrt to 3rd-party libraries conflicting with each other. – Sonneteer