philipp-classen / CppQuickCheck

QuickCheck-like property based automated testing tool for C++
BSD 2-Clause "Simplified" License
13 stars 5 forks source link

Header only version #8

Open philipp-classen opened 6 years ago

philipp-classen commented 6 years ago

At the moment, src/Arbitrary.cpp prevents the library from being used in a header-only style. Starting with C++17, this could be avoided.

Idea: For C++11, leave everything as it is, but for C++17, provide a single, header-only version of the library.

Advantage: In C++17 environments, this would simplify the installation.

BardurArantsson commented 6 years ago

Yes, this would be useful. I actually looked a bit into it -- I assume the idea would be to make the arbitrary bits "inline" with an inline definition? (Which is C++17 only.)

I wonder if a C++11 compatible workaround would be to change the "unGen" and "shrink" members into functions which return appropriate Arbitrary instances, e.g. something like:

template<>
struct ArbitraryImpl<float> {
  static const Arbitrary<float>::unGenType unGen() {
     static FloatArbitraryImpl instance();
     return instance;
  }
}

or something along those lines? AFAICT the static instance inside the function is guaranteed to work properly.

Of course it would quite a bit of work to rewrite all the call sites, but perhaps it wouldn't be too bad?

philipp-classen commented 6 years ago

You are right that I was thinking about "inline" (in C++17). The approach that you sketched should work, but I think the "inline" version is the better solution in the long run.

When it comes to support for older C++ versions, my current approach is to support older versions (C++11) as long as it is does not create too much effort. (Now that even Visual Studio supports C++14, it might be time to drop C++11 soon, and move on to C++14.)

If the header-only option is currently only available on a C++17 compiler, I think it would be fine. It would not break older compilers, as you could still install the library. On the other hand, if someone wants to try it out, at least on Linux, it is quite easy to get access to a C++17 compiler. New versions of Visual Studio also support "inline" now.

In my opinion, the project would benefit a lot from better documentation or a tutorial. That is my hidden agenda behind the single-file header-only version. :-) To make it easy for someone to try it out and experiment a bit. For that I'm fine with assuming a C++17 compiler.