Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a const reference to function type

Tags:

c++

c++11

#include <iostream>
#include <type_traits>

void func()
{

}

int main()
{
  using T = const decltype(func) &;
  using T2 = void (&)();
  std::cout << std::boolalpha << std::is_same_v<T, T2> << std::endl;
}

How do you declare a const reference to a function type? The statement above prints true so I'm assuming that the const in T is ignored somehow. Is it possible at all to declare a const reference to a function type?

like image 679
mkmostafa Avatar asked Jun 07 '18 09:06

mkmostafa


1 Answers

[dcl.fct]p7:

The effect of a cv-qualifier-seq in a function declarator is not the same as adding cv-qualification on top of the function type. In the latter case, the cv-qualifiers are ignored.

So you can only have a reference to a function, but not a const& as the const on a function type is ignored, as per above.

like image 116
Rakete1111 Avatar answered Oct 03 '22 18:10

Rakete1111