Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference of type between "fun" and "&fun"?

Do expressions fun and &fun have the same type or not?

Consider the following code:

template <typename Check, typename T> void check(T) {     static_assert(is_same<Check, T>::value); }  void fun() {}  check<void(*)()>(fun); check<void(*)()>(&fun);  cout << typeid(fun).name() << endl; cout << typeid(&fun).name() << endl; 

Both assertions succeed which suggests that both expressions have the same type. However, typeids return different results:

FvvE PFvvE 

Why is that?

like image 323
NPS Avatar asked Jan 23 '18 10:01

NPS


People also ask

What's the comparative of fun?

Funner and funnest have been in use as the comparative and superlative forms of the adjective fun for more than a century, though many people prefer to use more fun and most fun.

What type of word is fun?

Fun commonly functions as an adjective ("I had a fun time") and as a noun ("Let's have some fun"), and somewhat less commonly as a verb ("I'm just funning you").

Is fun a correct word?

As a noun, fun means enjoyment. Fun is not universally accepted as an adjective. People who do accept it as an adjective seem to prefer more fun and most fun over funner and funnest.

Why is funner not a word?

But if you're thinking that that logic is downright silly, most dictionary establishments agree with you. And they also agree that…the answer to “is funner a word?” is yes. If you want to consider “fun,” as an adjective, a word, then “funner” is indeed a word, as is “funnest,” per normal rules of adjective formation.


1 Answers

Both assertions succeed because they are applied to the type T deduced from function argument. In both cases it will be deduced as a pointer to function because functions decay to a pointer to function. However if you rewrite assertions to accept types directly then first one will fail:

static_assert(is_same<void(*)(), decltype(fun)>::value); static_assert(is_same<void(*)(), decltype(&fun)>::value); 

online compiler

like image 133
user7860670 Avatar answered Oct 13 '22 22:10

user7860670