简体   繁体   中英

Does the C++ standard library at any point include native headers per platform?

I'm in an argument with someone who is determined that the C++ standard library such as algorithm , memory , string etc. uses the Windows API header for performing some operations.

Is this the case or not? I understand that all Windows PEs include native DLLs from the WinAPI but I'm not sure that the standard library is at all compiler/os dependent.

The C++ standard library is a set of #include <> directives that, when inserted into a C++ program, have effects defined by the standard.

Technically they need not be files at all -- all #include <blah> directives mandated by the standard could instead be compiler intrinsics that modify the behavior of programs after the directive.

In practice, they are usually a mixture of pure C++ header file code, header file code that uses compiler intrinsics or platform-specific details, and libraries, where the libraries are implemented in a mixture of OS-specific, hardware-specific, or in C/C++.

There are parts of the C++ standard library that cannot be implemented in C++.

Short answer: yes, the standard library can (and does) makes use of platform specific headers.

Long answer: The interface exposed to the user is the same across platforms, but the implementation may make use of platform + compiler specific features.

Want proof? Find the include directory for your compiler, for example, the latest visual studio is here (by default):

C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\include

Go to this directory on the command line and run:

findstr -s -n -i -l "<windows.h>" *

On my machine, this outputs:

delayhlp.cpp:25:#include <windows.h>
msclr\marshal.h:20:#include <windows.h>
msclr\marshal_windows.h:18:#include <windows.h>
vccorlib.h:34:#include <windows.h>
vsgcapture.h:4:#include <Windows.h>

So yes, windows.h is included by multiple implementation files.


Edit in response to comments:

Examining the file "thread" in the visual studio include directory reveals the implementation of std::thread::join is:

inline void thread::join()
    {   // join thread
    if (!joinable())
        _Throw_Cpp_error(_INVALID_ARGUMENT);
    if (_Thr_is_null(_Thr))
        _Throw_Cpp_error(_INVALID_ARGUMENT);
    if (get_id() == _STD this_thread::get_id())
        _Throw_Cpp_error(_RESOURCE_DEADLOCK_WOULD_OCCUR);
    if (_Thrd_join(_Thr, 0) != _Thrd_success)
        _Throw_Cpp_error(_NO_SUCH_PROCESS);
    _Thr_set_null(_Thr);
}

The various _Thrd_XXX functions are defined in cthread.c , located in C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\crt\\src\\stl

For example, _Thrd_join is:

int _Thrd_join(_Thrd_t thr, int *code)
    {   /* return exit code when thread terminates */
    unsigned long res;
    if (WaitForSingleObjectEx(thr._Hnd, INFINITE, FALSE) == WAIT_FAILED
        || GetExitCodeThread(thr._Hnd, &res) == 0)
        return (_Thrd_error);
    if (code)
        *code = (int)res;
    return (CloseHandle(thr._Hnd) == 0 ? _Thrd_error : _Thrd_success);
}

Which uses various windows api functions, like WaitForSingleObjectEx

So it seems that although "windows.h" isn't visible to users of standard libary headers, it is used in the implementation of these functions.

Note that the implementation files (such as cthread.c) include "windows.h" via a file "wrapwin.h" in the same directory, which, according to comments in the file, is to suppress compiler warnings in "windows.h"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM