google / google/benchmark

[BUG] DoNotOptimize on values with const members again

Open
#1,997 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
10.4k
Forks
1.8k
Avg merge
2d 4h
Merged PRs (30d)
8

Description

**Describe the bug**
This is a follow-up to #764 . That one is closed, but I fail to see a fix there.

```
#include

struct X
{
X() : value{} {}

const int value;
};

template
void foo(T&& f)
{
X x;
f(x);
}

int main() {
foo([](const auto& i) { benchmark::DoNotOptimize(i); });
}
```
Playground (works with any recent gcc version): https://godbolt.org/z/Gz4vTo9ed

If the lambda parameter is declared `const auto&`:
`:18:51: warning: '...' is deprecated: The const-ref version of this method can permit undesired compiler optimizations in benchmarks [-Wdeprecated-declarations]`

If the lambda parameter is declared `auto&`:
`benchmark/benchmark.h:585:3: error: read-only reference 'value' used as 'asm' output`

How do I change my code so that `DoNotOptimize` works reliable and the compiler doesn't warn?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.