c++ – How should I require the C ++ 20 concept to have a member function with a specific return type?

Question: Question:

Af () returns an int, but if the code contains a commented out part, a compile error will occur if it is not bool. What should I do if I want to request this in a concept (I want the return value of f () to be bool)?

#include <iostream>
#include <string>
#include <concepts>

struct A{
    int f()
    {
        return 42;
    }
};

template<class T>
concept Printable = requires(T t){
    t.f();
};

template<typename T>
void PrintIfPrintable(T arg)
{
    if constexpr (Printable<T>){
        //bool a {arg.f()}; // コンパイルエラー
        std::cout << arg.f() << std::endl;
    }
}

int main()
{
    PrintIfPrintable(A());
}

Answer: Answer:

Does it feel like using the std::same_as concept to determine if the return value of tf() is bool ?

template<class T>
concept Printable = std::same_as<decltype(std::declval<T>().f()), bool>;
Scroll to Top