Search code examples
c++templatesc++17catch2std-variant

How to use TEMPLATE_TEST_CASE with pairs of types?


I am trying to use catch2 TEMPLATE_TEST_CASE for pairs of types, i.e. instead of templating a single type for each test, I need to use a correlated pair of types. I thought I could use std::variant to store these pairs, but compilation fails with: error: expected primary-expression before ‘)’ token. auto outtype = std::get<0>(TestType);.

I'd appreciate any help for the reason of this error or an alternative solution to this problem. Here is the code snippet:

    using varA = std::variant<OutputA, InputA>;
    using varB = std::variant<OutputB, InputB>;

    TEMPLATE_TEST_CASE("test", "[test][template]", varA, varB) {
    auto outtype = std::get<0>(TestType);
    auto intype  = std::get<1>(TestType);
    }

Solution

  • . instead of templating a single type for each test, I need to use a correlated pair of types.

    If is only a pair of types, I suppose you can use std::pair; std::tuple, for more types.

    I suppose you can try something as

    TEMPLATE_TEST_CASE("test", "[test][template]", std::pair<OutputA, InputA>, std::pair<OutputB, InputB>) {
      typename TestType::first_type outvalue = /* some initial value */;
      typename TestType::second_type invalue = /* some initial value */;
    }
    

    With std::tuple, to access the single types, you can use std::tuple_element, so

    TEMPLATE_TEST_CASE("test", "[test][template]", std::tuple<OutputA, InputA>, std::tuple<OutputB, InputB>) {
      std::tuple_element_t<0u, TestType> outvalue = /* some initial value */;
      std::tuple_element_t<1u, TestType> invalue = /* some initial value */;
    }