root icon indicating copy to clipboard operation
root copied to clipboard

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

Open bellenot opened this issue 1 year ago • 5 comments

Check duplicate issues.

  • [X] Checked for duplicates

Description

As described on the forum trying to '#include <atlstr.h>` 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

bellenot avatar Feb 06 '24 13:02 bellenot

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

vepadulano avatar Feb 08 '24 14:02 vepadulano

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 avatar Feb 08 '24 14:02 bellenot

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

bellenot avatar Mar 07 '24 08:03 bellenot

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

pcanal avatar Mar 07 '24 22:03 pcanal

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/

Related PR: https://github.com/root-project/root/issues/6411

ferdymercury avatar Mar 09 '24 09:03 ferdymercury