1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef BENCHMARK_UTILITIES_H
11#define BENCHMARK_UTILITIES_H
12
13#include <cassert>
14#include <type_traits>
15
16#include "benchmark/benchmark.h"
17
18namespace UtilitiesInternal {
19template <class Container>
20auto HaveDataImpl(int) -> decltype((std::declval<Container&>().data(), std::true_type{}));
21template <class Container>
22auto HaveDataImpl(long) -> std::false_type;
23template <class T>
24using HasData = decltype(HaveDataImpl<T>(0));
25} // namespace UtilitiesInternal
26
27template <class Container, std::enable_if_t<UtilitiesInternal::HasData<Container>::value>* = nullptr>
28void DoNotOptimizeData(Container& c) {
29 benchmark::DoNotOptimize(c.data());
30}
31
32template <class Container, std::enable_if_t<!UtilitiesInternal::HasData<Container>::value>* = nullptr>
33void DoNotOptimizeData(Container& c) {
34 benchmark::DoNotOptimize(&c);
35}
36
37#endif // BENCHMARK_UTILITIES_H
38

source code of libcxx/benchmarks/Utilities.h