I have seen code that looks like this:
struct foo_functor {
template <typename T, typename U>
constexpr auto operator()(T t, U u) const -> decltype(t | u) {
return t | u;
}
};
constexpr foo_functor foo;
As far as I can tell, it's the same as the following:
template <typename T, typename U>
constexpr auto foo(T t, U u) -> decltype(t | u) {
return t | u;
}
Why would you want to do the first one? Are there any differences? As far as I could see from the compiler output, at least with constexpr
, there wasn't. What about if they weren't constexpr
, would there be any differences in that case?
Edit: As a note, code very similar to the first example was seemingly being used in place of normal functions. 6 different structures, all with only operator()
templates, all were instantiated like the last line of the example. Each was then used exactly like a normal function.