Skip to content

[BUG] DoNotOptimize on values with const members again #1997

Description

@krzikalla

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

#include <benchmark/benchmark.h>

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

  const int value;
};

template<class T>
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&:
<source>: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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions