Could there be a race condition between __has_include () and subsequent #include?

Consider the following C ++ 1z code that uses __has_include()

:

#if __has_include(<optional>)
#  include <optional>
#  define have_optional 1
#else
#  define have_optional 0
#endif

      

Could there be a race condition between __has_include(<optional>)

and after #include <optional>

, or does the standard guarantee race-free behavior? For example, in the (unbelievable) situation where the header file is deleted immediately after checking __has_include()

, it will #include

unexpectedly fail.

+3


source to share


1 answer


Although I would say that this is a very implementation-specific issue, this #include

link
says

A __has_include

result 1

means only the header or source file with the specified name. This does not mean that the header or source file, if included, will not cause errors or contain anything useful.



So, you shouldn't count on a follow-up directive #include

.

The above link does indeed continue the above quote by noting that a compiler with C ++ 14 and C ++ 17 modes can have __has_include

as an extension of its C ++ 14 mode, a use case just <optional>

might result in the __has_include(<optional>)

following in C ++ 14 mode, but the actual one #include

doesn't work.

+4


source







All Articles