Home What is the difference between int () and int (*)()?
Reply: 1

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

Diego Teixeira de Souza
1#
Diego Teixeira de Souza Published in 2018-01-12 11:30:27Z

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 (*) ()
?

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;
}

SirGuy
2#
SirGuy Reply to 2018-01-12 13:19:24Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312017 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO