[Bug libstdc++/95545] thread:: conflicts with std::thread
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Jun 4 21:52:14 GMT 2020
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95545
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The problem is that 'using namespace std;' introduces an ambiguity. That is in
the user's code, there's nothing we can do in libstdc++ to avoid it.
We could add a different name for std::thread eg.
namespace __gnu_cxx { using thread = std::thread; }
and then users could be careful to always refer to __gnu_cxx::thread instead of
just thread.
But firstly, that is just a different portability problem, because that won't
work with the MSVC library or with libc++. And secondly, if you're going to be
careful and always refer to it with a qualified name, just use std::thread.
Referring to std::thread is portable and always works.
Alternatively, put the 'using namespace std;' at function scope, so that inside
the main() function name lookup finds std::thread, and never looks in the
global scope to find ::thread.
We could do:
namespace std { using __fred = thread; }
which would allow the code to have 'using namespace std;' at global scope, and
refer to __fred::hardware_concurrency() unqualified, but that's still not
portable.
The user code causes the problem, and the user code needs to fix it. There's no
libstdc++ bug and nothing libstdc++ can do that doesn't just shift the problem
somewhere else.
There are similar problems for any name in std:: which also exists at global
scope on some platforms, e.g. std::bind conflicts with POSIX bind(3).
It's well known that 'using namespace std;' causes problems like this. Even if
it works today, it might not work in future because new versions of the C++
standard will add new names to namespace std, so 'using namespace std;' is
basically asking for an unbounded set of names to be dropped into the global
namespace, with potential collisions.
Either refer to 'std::thread' explicitly or introduce the name 'thread' into a
more narrow scope than the one that already has the AIX 'thread'.
More information about the Gcc-bugs
mailing list