Commit 6d8339dd by Eric Committed by GitHub

Fix #444 - Use BENCHMARK_HAS_CXX11 over __cplusplus. (#446)

* Fix #444 - Use BENCHMARK_HAS_CXX11 over __cplusplus. MSVC incorrectly defines __cplusplus to report C++03, despite the compiler actually providing C++11 or greater. Therefore we have to detect C++11 differently for MSVC. This patch uses `_MSVC_LANG` which has been defined since Visual Studio 2015 Update 3; which should be sufficient for detecting C++11. Secondly this patch changes over most usages of __cplusplus >= 201103L to check BENCHMARK_HAS_CXX11 instead. * remove redunant comment
parent 2a05f248
...@@ -185,7 +185,7 @@ BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10); ...@@ -185,7 +185,7 @@ BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10);
Three macros are provided for adding benchmark templates. Three macros are provided for adding benchmark templates.
```c++ ```c++
#if __cplusplus >= 201103L // C++11 and greater. #ifdef BENCHMARK_HAS_CXX11
#define BENCHMARK_TEMPLATE(func, ...) // Takes any number of parameters. #define BENCHMARK_TEMPLATE(func, ...) // Takes any number of parameters.
#else // C++ < C++11 #else // C++ < C++11
#define BENCHMARK_TEMPLATE(func, arg1) #define BENCHMARK_TEMPLATE(func, arg1)
......
...@@ -164,7 +164,8 @@ BENCHMARK(BM_test)->Unit(benchmark::kMillisecond); ...@@ -164,7 +164,8 @@ BENCHMARK(BM_test)->Unit(benchmark::kMillisecond);
#define BENCHMARK_BENCHMARK_H_ #define BENCHMARK_BENCHMARK_H_
#if __cplusplus >= 201103L // The _MSVC_LANG check should detect Visual Studio 2015 Update 3 and newer.
#if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
#define BENCHMARK_HAS_CXX11 #define BENCHMARK_HAS_CXX11
#endif #endif
...@@ -921,7 +922,7 @@ class Fixture : public internal::Benchmark { ...@@ -921,7 +922,7 @@ class Fixture : public internal::Benchmark {
#define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \ #define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \
BENCHMARK(n)->RangePair({{(l1), (h1)}, {(l2), (h2)}}) BENCHMARK(n)->RangePair({{(l1), (h1)}, {(l2), (h2)}})
#if __cplusplus >= 201103L #ifdef BENCHMARK_HAS_CXX11
// Register a benchmark which invokes the function specified by `func` // Register a benchmark which invokes the function specified by `func`
// with the additional arguments specified by `...`. // with the additional arguments specified by `...`.
...@@ -941,7 +942,7 @@ class Fixture : public internal::Benchmark { ...@@ -941,7 +942,7 @@ class Fixture : public internal::Benchmark {
#func "/" #test_case_name, \ #func "/" #test_case_name, \
[](::benchmark::State& st) { func(st, __VA_ARGS__); }))) [](::benchmark::State& st) { func(st, __VA_ARGS__); })))
#endif // __cplusplus >= 11 #endif // BENCHMARK_HAS_CXX11
// This will register a benchmark for a templatized function. For example: // This will register a benchmark for a templatized function. For example:
// //
...@@ -962,7 +963,7 @@ class Fixture : public internal::Benchmark { ...@@ -962,7 +963,7 @@ class Fixture : public internal::Benchmark {
new ::benchmark::internal::FunctionBenchmark(#n "<" #a "," #b ">", \ new ::benchmark::internal::FunctionBenchmark(#n "<" #a "," #b ">", \
n<a, b>))) n<a, b>)))
#if __cplusplus >= 201103L #ifdef BENCHMARK_HAS_CXX11
#define BENCHMARK_TEMPLATE(n, ...) \ #define BENCHMARK_TEMPLATE(n, ...) \
BENCHMARK_PRIVATE_DECLARE(n) = \ BENCHMARK_PRIVATE_DECLARE(n) = \
(::benchmark::internal::RegisterBenchmarkInternal( \ (::benchmark::internal::RegisterBenchmarkInternal( \
......
...@@ -109,7 +109,7 @@ BENCHMARK_TEMPLATE2(BM_Sequential, std::vector<int>, int) ...@@ -109,7 +109,7 @@ BENCHMARK_TEMPLATE2(BM_Sequential, std::vector<int>, int)
->Range(1 << 0, 1 << 10); ->Range(1 << 0, 1 << 10);
BENCHMARK_TEMPLATE(BM_Sequential, std::list<int>)->Range(1 << 0, 1 << 10); BENCHMARK_TEMPLATE(BM_Sequential, std::list<int>)->Range(1 << 0, 1 << 10);
// Test the variadic version of BENCHMARK_TEMPLATE in C++11 and beyond. // Test the variadic version of BENCHMARK_TEMPLATE in C++11 and beyond.
#if __cplusplus >= 201103L #ifdef BENCHMARK_HAS_CXX11
BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512); BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512);
#endif #endif
...@@ -197,7 +197,7 @@ static void BM_ManualTiming(benchmark::State& state) { ...@@ -197,7 +197,7 @@ static void BM_ManualTiming(benchmark::State& state) {
BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseRealTime(); BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseRealTime();
BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseManualTime(); BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseManualTime();
#if __cplusplus >= 201103L #ifdef BENCHMARK_HAS_CXX11
template <class... Args> template <class... Args>
void BM_with_args(benchmark::State& state, Args&&...) { void BM_with_args(benchmark::State& state, Args&&...) {
...@@ -213,7 +213,7 @@ void BM_non_template_args(benchmark::State& state, int, double) { ...@@ -213,7 +213,7 @@ void BM_non_template_args(benchmark::State& state, int, double) {
} }
BENCHMARK_CAPTURE(BM_non_template_args, basic_test, 0, 0); BENCHMARK_CAPTURE(BM_non_template_args, basic_test, 0, 0);
#endif // __cplusplus >= 201103L #endif // BENCHMARK_HAS_CXX11
static void BM_DenseThreadRanges(benchmark::State& st) { static void BM_DenseThreadRanges(benchmark::State& st) {
switch (st.range(0)) { switch (st.range(0)) {
......
...@@ -8,6 +8,10 @@ ...@@ -8,6 +8,10 @@
#error C++11 or greater detected. Should be C++03. #error C++11 or greater detected. Should be C++03.
#endif #endif
#ifdef BENCHMARK_HAS_CXX11
#error C++11 or greater detected by the library. BENCHMARK_HAS_CXX11 is defined.
#endif
void BM_empty(benchmark::State& state) { void BM_empty(benchmark::State& state) {
while (state.KeepRunning()) { while (state.KeepRunning()) {
volatile std::size_t x = state.iterations(); volatile std::size_t x = state.iterations();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment