xmllmx
xmllmx

Reputation: 42379

Is `int(int) const` a valid function type in C++23?

Below is excerpted from cppref:

template<class...>
class move_only_function; // not defined

template<class R, class... Args>
class move_only_function<R(Args...) const>;

I also tried the following code:

// ok
std::function<int(int)> fn; 

// error: implicit instantiation of undefined template
// 'std::function<void () const>'
std::function<int(int) const> fn; 

Is int(int) const a valid function type in C++23?

Upvotes: 2

Views: 190

Answers (2)

Alan
Alan

Reputation: 1

Is int(int) const a valid function type in C++23?

Yes, int(int) const has always been(even before c++23) a function type. Even auto(int)const->int is a function type.

This can be seen from dcl.fct:

  1. In a declaration T D where D has the form
 D1 ( parameter-declaration-clause ) cv-qualifier-seqopt
   ref-qualifieropt noexcept-specifieropt attribute-specifier-seqopt
  1. In a declaration T D where D has the form
 D1 ( parameter-declaration-clause ) cv-qualifier-seqopt
   ref-qualifieropt noexcept-specifieropt attribute-specifier-seqopt trailing-return-type  
  1. A type of either form is a function type.

(emphasis mine)

This means that both are auto(int)const->int as well as int(int)const are function type.

using type = int(int) const;             //this is a function type
using anothertype = auto(int)const->int; //this is also a function-type

Upvotes: 4

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 123114

By chance I found this example from the standard draft:

[Example 4: 
typedef int FIC(int) const;
FIC f;              // error: does not declare a member function
struct S {
  FIC f;            // OK
};
FIC S::*pm = &S::f; // OK
— end example]

The section is about the possibility to use an alias to declare but not define a (member) function. Coincidentally, it nicely illustrates the foo(bar) const type.

Above FIC is an alias for int(int) const. Its a function type. There are, however, no functions of that type. You can use it to declare a member inside a class, or declare a pointer to member function, as shown in the lines marked with OK respectively. The type of the pointer is int(S::*)(int)const.

Upvotes: 4

Related Questions