Usage of concepts with non-type template parameter packs in boolean contexts

306 views Asked by At

In C++20, I defined a concept AllIntegral with a non-type template parameter pack auto... T_values. Whilst, GCC 10.1.0 accepts its usage in some contexts, it refuses to compile its usage in others, particularly in an if statement. The associated error message says 'AllIntegral' does not constrain a type.

My code looks like this:

#include <concepts>
#include <ios>
#include <iostream>

template<auto... T_values>
concept AllIntegral = (std::integral<decltype(T_values)> && ...);

int main()
{
    std::cout << std::boolalpha << AllIntegral<1, 2> << '\n';  // compiles and prints "true"
    if (AllIntegral<1, 2>) std::cout << "true" << '\n';        // does not compile

    std::cout.flush();
}

This is the compiler output:

main.cpp: In function ‘int main()’:
main.cpp:11:9: error: ‘AllIntegral’ does not constrain a type
   11 |     if (AllIntegral<1, 2>) std::cout << "true" << '\n';
      |         ^~~~~~~~~~~~~~~~~
main.cpp:6:9: note: concept defined here
    6 | concept AllIntegral = (std::integral<decltype(T_values)> && ...);

What's the reason for this error? Why is my concept not usable in a boolean context?

0

There are 0 answers