What is the difference between int () and int (*) ()?

I am trying to create a class template that identifies functions in which I can determine when a function specializes the class model for R (*) () , but in std::function you can declare return_type () and std::is_same< int(), int (*) () >.::value 0.

What does this int () operator mean and what is the difference between int () and
int (*) ()

int (*) ()
?

Updated: So int () is a function declaration or function type and int (*)() is a pointer to a function. But is waht a function of type int (std::string::)() :) int (std::string::)() ? Is it something like int std::string::() or like in std::function int(const std::string&) ? How can I make this program output 1?
 #include <iostream> template<typename A,typename B> struct IsSame{ enum{ value = 0}; }; template<typename A> struct IsSame<A,A>{ enum{ value = 1 }; }; typedef int (SumType)(int,int)const; class X{ public: SumType sum; }; int X::sum(int a,int b)const{ return a+b; } int main() { std::cout << IsSame< int (const std::string&,int,int)const, decltype( &X::sum)>::value; } 

>

+5
source share
1 answer

The function is of type void() , and if you take the address of it, you get a pointer to this function void(*)() . They are not the same type, although a function can decay to a pointer to a function in the same way that arrays decay to pointers to the first member.

This means that you can declare a function:

 void f(); 

and assign it to a function pointer (of the same signature):

 void (*p_to_f)() = f; 

and the function decays into a pointer to a function.
That's why I think it may be difficult to understand the difference between void() and void(*)() , but they are different types, and this difference can be important in templates (where this decay does not necessarily occur).

One important example of when decay does not occur is the matching of specialized patterns. Consider what std::function does, for example:

 template <class> class function; template <class R, class ... Args> class function<R(Args...)> { /* ... stuff ... */ }; 

The fact that specialization refers to the type of function is different from whether it was specialized in the type of function pointer.

 function<void(int,int)> f;//matches specialisation function<void(*)(int,int)> g;//does not match specialisation 

In response to an OP comment (now deleted):
To also combine pointers to functions that you can perform:

 template <class R, class ... Args> function<R(*)(Args...)> : function<R(Args...>{}; 

which essentially means that you are considering functions and function pointers are the same. If you want to combine as many functions as you can, you also need to consider member functions, member functions, and noexcept functions (as in C ++ 17). If you want to be completely thorough, there are also & , && , const & , const&& , volatile etc., but I wouldn’t worry about them if they really didn’t come.

+8
source

Source: https://habr.com/ru/post/1274658/


All Articles