2 ms·
that only works if you are in a context where you can deduce the type from ctor arguments, which is definitely not all cases (for instance, storing as a member
by jcelerier 6y ago
that only works if you are in a context where you can deduce the type from ctor arguments, which is definitely not all cases (for instance, storing as a member and initializing in a ctor, etc...)
- gpderetta 6y agoYou can then specify the function pointer type, although mildly inconvenient. If you want to use lambdas though, then yes, you are out of luck as their type is unnameable.
- jcelerier 6y agobut that fails as soon as you have a case where you want to pass an algorithm implementation that does have state. That was my point - whenever I assumed that I would not have state... well I ended up needing to refactor everything because I ended up needing to add a new strategy that did require some state later. In the context of C++, struct / class types are more generic that function pointers and there's no real shortcut around this - that's how the language is specified.
- steerablesafe 6y agoThere is nothing wrong with writing generic code that works both with functions and function objects. Note that stateless (empty) function objects can often be more efficient than function pointers.