Amir Kirsh
Amir Kirsh

Reputation: 13760

Problem with requires clause inside if constexpr

While trying to implement if constexpr with requires clause based on if constexpr and requires-expression for ad-hoc concepts checking facing the following problem:

template<class P>
concept TuplePair = requires(P p) {
    requires std::tuple_size<P>::value == 2;
    std::get<0>(p);
    std::get<1>(p);
};

void print(const auto& p) {
    if constexpr( TuplePair<decltype(p)> ) {
        std::cout << std::get<0>(p) << ", " << std::get<1>(p) << std::endl;
    }
    else {
        std::cout << "else" << std::endl;
    }
}

int main() {
    // justifiably prints 'else':
    print(std::make_tuple(3, 4, 5));

    // prints 'else' even though this is a valid TuplePair:
    print(std::make_tuple(1, 2));
}

What's wrong with the if constexpr requires clause?

Upvotes: 4

Views: 820

Answers (1)

p is a reference, and as such decltype(p) is a reference type. For a reference type std::tuple_size will not work correctly. So the concept check doesn't pass. You can employ std::remove_cvref_t to obtain the plain referent type

TuplePair<std::remove_cvref_t<decltype(p)>>

Live Demo

Upvotes: 5

Related Questions