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 ()
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;
}
int means a variable whose datatype is integer. sizeof(int) returns the number of bytes used to store an integer. int* means a pointer to a variable whose datatype is integer. sizeof(int*) returns the number of bytes used to store a pointer.
There is no such difference in between these two types of array declaration. It's just what you prefer to use, both are integer type arrays. There is no difference in functionality between both styles of declaration.
In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.
The int type in C is a signed integer, which means it can represent both negative and positive numbers. This is in contrast to an unsigned integer (which can be used by declaring a variable unsigned int), which can only represent positive numbers.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With