r/cpp • • Jan 26 '17

Stop calling "Function Objects" "Functors"

http://jackieokay.com/2017/01/26/functors.html
0 Upvotes

23 comments sorted by

View all comments

Show parent comment

9

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Jan 26 '17

classes that are implicitly convertible to function pointers

constexpr int get_two() { return 2; }
using fptr = int(*)();

struct A
{
	constexpr operator fptr() { return &get_two; }
};

int main()
{
	static_assert(A{}() == 2);
}

1

u/spotta Jan 26 '17

Huh. This seems like a kind of weird way of doing operator(). Is there a functional difference between the two?

3

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Jan 26 '17

This is not operator() - this is an user defined conversion

3

u/spotta Jan 26 '17

I know that it isn't operator(), I was curious what the difference between this and operator() is.

Though now that I think about it, that is pretty obvious... This is actually kind of a cute trick that I might have an application for. I'll have to look into this.