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

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

user10580
1#
user10580 Published in September 19, 2018, 9:09 am

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

share|improve this question
  • 3
    One is a function and another is pointer to a function. – HolyBlackCat Jan 12 at 11:35
  • But what is the use of int ()? So int () is declaration of a function signature and int (*) () is pointer to function. – Diego Teixeira de Souza Jan 12 at 11:50
  • 1
    There is also the equivalent for non static member function :What type is int(int)& or int(int) const &? – Oliv Jan 12 at 13:11
  • 1
    @Diego Teixeira de Souza int (std::string::*) () is a pointer to a non-static member function in the class std::string that takes no arguments and returns an int. I know, it's crazy – Zebrafish Jan 12 at 13:39
  • 1
    Zebrafish is right. C++ inherits and extends its declarator syntax from C, all the way back to "K&R". The idea was to use a syntax close to the one at the point of use. So int *f() is a function returning a pointer to int because *f() is what one use to call such a function and deref the result. Likewise, int (*f)() is a pointer to a function returning an int because you would first deref the pointer and then call the function (nevermind that this first step is syntactically optional). Read declarators inside out and consider precedence, e.g. int *a[4] is an array, not a pointer. – Arne Vogel Jan 12 at 14:13

1 Answer 1

active oldest votes
up vote 8 down vote
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO