简体   繁体   中英

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

I'm trying to create a class template that identifies functions, in which I can identify when a function is specializing the class model for R (*) () , but in std::function you can declare return_type () , and std::is_same< int(), int (*) () >.::value is zero.

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

int (*) ()
?

Updated: So int () is function declaration or function type and int (*)() is pointer to function.But waht is the type function of int (std::string:: )() ?It's something like int std::string:: () , or like in std::function int(const std::string&) ?How i can 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; } 

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

This means you can declare a function:

void f();

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

void (*p_to_f)() = f;

and the function decays to a pointer to a function.
This is why I think it can be difficult to understand the difference between void() and void(*)() , but they are distinct types, and that difference can be important in templates (where this decay doesn't necessarily occur).

One important example of when decay doesn't happen is when matching template specialisations. Consider what std::function does for example:

template <class>
class function;

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

The fact that the specialisation is on a function type is different than if it were specialised on a pointer-to-function type.

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

In response to the OP's comment (now deleted):
In order to also match pointers to functions you can do:

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

which essentially means you treat functions and pointers to functions the same. If you want to match as many functions as you can, you also have to consider member functions, const member functions and noexcept functions (as of C++17). If you want to be completely thorough, there's also & , && , const & , const&& , volatile , etc... but I wouldn't worry about those unless they actually come up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM