2022-07-30 C++20 concepts for testing Tags: c++ conceptsWhen you'd like to write a test that checks whether a piece of code compiles with given template parameters, use concepts: ``` template<typename T, typename U> concept Addable = requires (T a, U b) { a + b; }; REQUIRE(!Addable<std::vector<float>, int>) ``` However this doesn't always work, see the included example. The catch is when there is a function in the _immediate context_ that takes the two parameters `[1]`, but doesn't compile down the line. A remedy is to add an explicit requirement to this function `[2]`. Demo: https://godbolt.org/z/5WM7denjK Related discussion: https://stackoverflow.com/questions/70940008/concept-requirement-and-non-immediate-context