-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Description
DoNotOptimize produces an error when used on a variable with a const member:
$ clang++ --version
clang version 9.0.0
$ cat <<EOF | clang++ -xc++ - -isystem <path-to-google-benchmark> -std=c++17
#include <benchmark/benchmark.h>
struct Foo { int const data = 0; };
int main() {
Foo foo{};
benchmark::DoNotOptimize(foo);
}
EOFThe output is:
In file included from <stdin>:1:
<snip>/include/benchmark/benchmark.h:322:28: error: invalid lvalue in asm
output
asm volatile("" : "+r,m"(value) : : "memory");
^~~~~
<stdin>:12:16: note: in instantiation of function template specialization 'benchmark::DoNotOptimize<Foo>' requested
here
benchmark::DoNotOptimize(foo);
^
1 error generated.
I assume this is because Clang looks at the output operands of the inline assembly and sees that one of the members of foo is const, and thus can't be written to. This used to work until Clang 6 if I'm not mistaken. I'm not sure this is an actual problem with Google Benchmark, but could you please suggest an alternative? I can think of the following:
- Use
ClobberMemory()instead. This is a bigger hammer than I'd like, however. - Pass a
char*to the object instead, like so:I'm not sure this has the intended effect.Foo foo{}; char* foop = reinterpret_cast<char*>(&foo); benchmark::DoNotOptimize(*foop); - Do not make the member
const. This is not really an option in my case.
Metadata
Metadata
Assignees
Labels
No labels