Home>

I am trying to determine if a function with default arguments can be called in requirements, but the default arguments do not seem to be taken into account.
I understand that the type is void (* fp) (int, int), so when I write like the following code, it becomes false.
So I don't know how to write the default argument. Does it exist in the first place?

# include<type_traits>
#include<iostream>
template<typename T>
concept C = requires (T x)
{
    x (10);
};
void f (int, int = 1) {}
int main ()
{
    std :: cout<<std :: boolalpha<<C<decltype (f)><<std :: endl;// false is output
    return 0;
}
c++
  • Answer # 1

    There is no default argument information in the type information, so no matter how the type is manipulated, it is not possible to make a judgment considering the default argument.

    A function with a default argument is lost as an argument to another function or as a template argument at that point.

    // Example where default argument is lost and error occurs
    #include
    void f (int, int = 1) {}
    template 
    void foo (void) {
      f (10);
    }
    template
    void bar (T&f) {
      f (10);
    }
    int main (void) {
      foo();
      bar (f);
      return 0;
    }

    So if the concept takes into account the default arguments, it will probably not fit.

    It seems to be a deliberate design that default arguments are not considered in the

    concept.