root-project / root

The official repository for ROOT: analyzing, storing and visualizing big data, scientifically
https://root.cern
Other
2.64k stars 1.26k forks source link

The implicit `using namespace std;` can have nasty side effects on Windows. #14602

Open bellenot opened 7 months ago

bellenot commented 7 months ago

Check duplicate issues.

Description

As described on the forum trying to '#include ` in ROOT create a lot of errors:

root [0] #include <atlstr.h>
In file included from ROOT_prompt_0:1:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\ATLMFC\include\atlstr.h:19:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\ATLMFC\include\atlbase.h:66:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\ATLMFC\include\atlcore.h:25:
In file included from C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\Ole2.h:37:
In file included from C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objbase.h:16:
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared\rpcndr.h:192:9: error: reference to 'byte' is ambiguous
typedef byte cs_byte;
        ^
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared\rpcndr.h:191:23: note: candidate found by name lookup is 'byte'
typedef unsigned char byte;
                      ^
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstddef:28:12: note: candidate found by name lookup is 'std::byte'
enum class byte : unsigned char {};
           ^
In file included from ROOT_prompt_0:1:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\ATLMFC\include\atlstr.h:19:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\ATLMFC\include\atlbase.h:66:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\ATLMFC\include\atlcore.h:25:
In file included from C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\Ole2.h:37:
In file included from C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um\objbase.h:16:
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared\rpcndr.h:962:6: error: reference to 'byte' is ambiguous
     byte               *   pNetworkData,
     ^

[...]

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstddef:28:12: note: candidate found by name lookup is 'std::byte'
enum class byte : unsigned char {};
           ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
root [1]

To illustrate this, compiling this code:

#include <iostream>
using namespace std;
#include <atlstr.h>

int main() {
    std::cout << "hello world!" << std::endl;
    return 0;
}

is enough to generate the errors:

C:\Users\bellenot\rootdev>cl -nologo -MD -GR -EHsc -Zc:__cplusplus -std:c++17 atlstr.cxx
atlstr.cxx
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared\rpcndr.h(192): error C2872: 'byte': ambiguous symbol
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared\rpcndr.h(191): note: could be 'unsigned char byte'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstddef(28): note: or       'std::byte'
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared\rpcndr.h(962): error C2872: 'byte': ambiguous symbol
C:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared\rpcndr.h(191): note: could be 'unsigned char byte'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\cstddef(28): note: or       'std::byte'

[...]

And when removing the using namespace std; line, there is no error

Reproducer

On Windows:

root [0] #include <atlstr.h>

ROOT version

Any

Installation method

build from source

Operating system

Windows

Additional context

No response

vepadulano commented 7 months ago

I thought we got rid of the implicit using namespace std?

bellenot commented 7 months ago

I thought we got rid of the implicit using namespace std?

I don't know, but that's not good enough anyway (Cling fails too, but with another error) And:

root [1] cout << "hello" << endl;
hello
root [2]
bellenot commented 6 months ago

I thought we got rid of the implicit using namespace std?

See https://github.com/root-project/root/blob/master/core/metacling/src/TCling.cxx#L1335-L1336

pcanal commented 6 months ago

Yes the PR that removes it is still in Draft mode https://github.com/root-project/root/pull/11027 and needs help.

ferdymercury commented 6 months ago

As a suggestion, if we want to deprecate 'using namespace std' in the long term, we could follow what was being proposed here with 'auto': https://github.com/root-project/root/pull/14645/