I would like to use thread_local
keyword in my project. However, not all the compiler that supports -std=c++11
supports thread_local
. Can I know whether there's a way to determine whether thread_local
is supported on compile time? I tried googling it but didn't find the answer.

- 5,814
- 7
- 40
- 79
-
1[This](http://stackoverflow.com/questions/5047971/how-do-i-check-for-c11-support) wont tell you if `thread_local` is supported but you can tell if C++11 is being used. – NathanOliver Jan 15 '16 at 19:57
-
The usual way is to use thread_local, if it does not compile then grab whatever macros identify given compiler and use ifdef-s to replace it with something that works. – marcinj Jan 15 '16 at 20:02
-
5The usual technique (with linux installation packages) is to run a configure script that tries to compile a small code snippet and checks the compiler's exit status. A configuration header file is then generated containing a number of defines that tell you what can be used in your code, and what cannot be used. – πάντα ῥεῖ Jan 15 '16 at 20:02
-
@luskan That's a bit shortcut and clumsy. – πάντα ῥεῖ Jan 15 '16 at 20:03
-
3I don't think it's an optional part of the standard, so you are sort of asking for a standardized way to find out if the compiler adheres to the standard or not. I think you are down to a bunch of #ifdefs, or make configure. – Martin Bonner supports Monica Jan 15 '16 at 20:07
-
@MartinBonner [Agreed](http://en.cppreference.com/w/cpp/language/storage_duration) – πάντα ῥεῖ Jan 15 '16 at 20:09
1 Answers
However, not all the compiler that supports
-std=c++11
supportsthread_local
.
According to this reference they need to, to be c++11 standard compliant.
So your question basically boils down to determine if parts of your code are compiled with the -std=c++11
option or not, which is already well answered here.
If you see differences with particular OS environments/C++ Compiler implementations, I'd recommend to run a pre-compilation script, that probes for particular compiler capabilities, and generate a config.h
header for you.
Including this header can be used to determine, if particular features are available and you can place appropriate
#if defined(FEATURE)
// feature specific code
#else
// plain code
#endif
in your header and source files.
To support the latter option I have introduced, there are the GNU automake tools, which support to generate such probing script. It's available to support a broad variety of target platforms.
-
1your answer suggests you have never compiled on a mac with clang after the clowns at Apple have done their best to destroy it. :/ – Richard Hodges Jan 15 '16 at 20:38
-
1@RichardHodges No I didn't actually. But it's actually required to be implemented by c++11 compliant compilers. So those you mention have bugs? That would require additional `#ifdef` to sort out unless that's gonna be fixed. – πάντα ῥεῖ Jan 15 '16 at 20:40
-
@πάνταῥεῖ, there is at least one much used thing outside which is supposed to be c++11 conformant, yet misses all thread-special parts of C++11. Namely, mingw and the version of gcc they provide. – SergeyA Jan 15 '16 at 21:02
-
@SergeyA Fair point. But still necessary to sort these out specifcally from system configurations, no? – πάντα ῥεῖ Jan 15 '16 at 21:05
-
-
clang-700.1.81 on Apple for example, does support -std=c++11 but not support thread_local. – keelar Jan 17 '16 at 06:22
-
#if defined(FEATURE) does not work actually, it only works for macro. See https://gcc.gnu.org/onlinedocs/cpp/Defined.html – keelar Apr 01 '16 at 22:31