Skip to content

Commit 6d8339d

Browse files
authored
Fix google#444 - Use BENCHMARK_HAS_CXX11 over __cplusplus. (google#446)
* Fix google#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
1 parent 2a05f24 commit 6d8339d

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10);
185185
Three macros are provided for adding benchmark templates.
186186
187187
```c++
188-
#if __cplusplus >= 201103L // C++11 and greater.
188+
#ifdef BENCHMARK_HAS_CXX11
189189
#define BENCHMARK_TEMPLATE(func, ...) // Takes any number of parameters.
190190
#else // C++ < C++11
191191
#define BENCHMARK_TEMPLATE(func, arg1)

include/benchmark/benchmark.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ BENCHMARK(BM_test)->Unit(benchmark::kMillisecond);
164164
#define BENCHMARK_BENCHMARK_H_
165165

166166

167-
#if __cplusplus >= 201103L
167+
// The _MSVC_LANG check should detect Visual Studio 2015 Update 3 and newer.
168+
#if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
168169
#define BENCHMARK_HAS_CXX11
169170
#endif
170171

@@ -921,7 +922,7 @@ class Fixture : public internal::Benchmark {
921922
#define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \
922923
BENCHMARK(n)->RangePair({{(l1), (h1)}, {(l2), (h2)}})
923924

924-
#if __cplusplus >= 201103L
925+
#ifdef BENCHMARK_HAS_CXX11
925926

926927
// Register a benchmark which invokes the function specified by `func`
927928
// with the additional arguments specified by `...`.
@@ -941,7 +942,7 @@ class Fixture : public internal::Benchmark {
941942
#func "/" #test_case_name, \
942943
[](::benchmark::State& st) { func(st, __VA_ARGS__); })))
943944

944-
#endif // __cplusplus >= 11
945+
#endif // BENCHMARK_HAS_CXX11
945946

946947
// This will register a benchmark for a templatized function. For example:
947948
//
@@ -962,7 +963,7 @@ class Fixture : public internal::Benchmark {
962963
new ::benchmark::internal::FunctionBenchmark(#n "<" #a "," #b ">", \
963964
n<a, b>)))
964965

965-
#if __cplusplus >= 201103L
966+
#ifdef BENCHMARK_HAS_CXX11
966967
#define BENCHMARK_TEMPLATE(n, ...) \
967968
BENCHMARK_PRIVATE_DECLARE(n) = \
968969
(::benchmark::internal::RegisterBenchmarkInternal( \

test/benchmark_test.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ BENCHMARK_TEMPLATE2(BM_Sequential, std::vector<int>, int)
109109
->Range(1 << 0, 1 << 10);
110110
BENCHMARK_TEMPLATE(BM_Sequential, std::list<int>)->Range(1 << 0, 1 << 10);
111111
// Test the variadic version of BENCHMARK_TEMPLATE in C++11 and beyond.
112-
#if __cplusplus >= 201103L
112+
#ifdef BENCHMARK_HAS_CXX11
113113
BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512);
114114
#endif
115115

@@ -197,7 +197,7 @@ static void BM_ManualTiming(benchmark::State& state) {
197197
BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseRealTime();
198198
BENCHMARK(BM_ManualTiming)->Range(1, 1 << 14)->UseManualTime();
199199

200-
#if __cplusplus >= 201103L
200+
#ifdef BENCHMARK_HAS_CXX11
201201

202202
template <class... Args>
203203
void BM_with_args(benchmark::State& state, Args&&...) {
@@ -213,7 +213,7 @@ void BM_non_template_args(benchmark::State& state, int, double) {
213213
}
214214
BENCHMARK_CAPTURE(BM_non_template_args, basic_test, 0, 0);
215215

216-
#endif // __cplusplus >= 201103L
216+
#endif // BENCHMARK_HAS_CXX11
217217

218218
static void BM_DenseThreadRanges(benchmark::State& st) {
219219
switch (st.range(0)) {

test/cxx03_test.cc

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#error C++11 or greater detected. Should be C++03.
99
#endif
1010

11+
#ifdef BENCHMARK_HAS_CXX11
12+
#error C++11 or greater detected by the library. BENCHMARK_HAS_CXX11 is defined.
13+
#endif
14+
1115
void BM_empty(benchmark::State& state) {
1216
while (state.KeepRunning()) {
1317
volatile std::size_t x = state.iterations();

0 commit comments

Comments
 (0)