gapry / algorism

MIT License
0 stars 0 forks source link

Google Benchmark Incompatibility with C++ Modules as Template Arguments #4

Open gapry opened 3 months ago

gapry commented 3 months ago

I have encountered an issue when trying to use C++ modules with Google Benchmark. Specifically, Google Benchmark fails to compile when attempting to use a class from a C++ module as a template argument.

You can use the following design to reproduce the issue:

#include <benchmark/benchmark.h>

import algorism.string;

template<typename TString>
static void StringAppendCStr(benchmark::State& state) {
  const char* const s1 = "Hi Hi! ";
  const char* const s2 = "I go to school by bus!";

  TString bench_str(s1);
  for (auto _ : state) {
    bench_str += s2;
  }
}

static void BM_Algorism_StringAppendCStr(benchmark::State& state) {
  StringAppendCStr<algorism::string>(state);
}

static void BM_STD_StringAppendCStr(benchmark::State& state) {
  StringAppendCStr<std::string>(state);
}

BENCHMARK(BM_Algorism_StringAppendCStr)->Iterations(1024);
BENCHMARK(BM_STD_StringAppendCStr)->Iterations(1024);

The corresponding error report:

error: no matching function for call to 'StringAppendCStr'
StringAppendCStr<algorism::string>(state);

At the moment, I am using the following workaround by not using the templates with the module class and instead defining benchmark functions for each type:

void BM_Algorism_StringAppendCStr(benchmark::State& state) {
  const char* const s1 = "Hi Hi! ";
  const char* const s2 = "I go to school by bus!";

  algorism::string bench_str(s1);
  for (auto _ : state) {
    bench_str += s2;
  }
}
BENCHMARK(BM_Algorism_StringAppendCStr)->Iterations(1024);

void BM_STD_StringAppendCStr(benchmark::State& state) {
  const char* const s1 = "Hi Hi! ";
  const char* const s2 = "I go to school by bus!";

  std::string bench_str(s1);
  for (auto _ : state) {
    bench_str += s2;
  }
}
BENCHMARK(BM_STD_StringAppendCStr)->Iterations(1024);

I mention it here because it's not critical at the moment. I'll try to fix it when I have time.

gapry commented 3 months ago

read the PR https://github.com/gapry/algorism/pull/5