I have a Java code where the return type of a function has unbounded wildcard type (?). How can I emulate something like this in C++? e.g.
public GroupHandlerSetting<?> handleGroupProcessingFor(final EventHandler<T> eventHandler)
{
return new GroupHandlerSetting<T>(eventHandler, eventProcessors);
}
In C++ all type arguments must have a name, whether you use it or not, so there is no question mark. Just make it a template argument to the function and give it a name and you should be fine.
That’s the trivial part, the more complex part is enforcing constraints on the type, and for that you can use SFINAE:
That is using C++11 for the SFINAE, in C++03, it is a bit more convoluted (as if this version was simple) as you cannot use SFINAE on a function template argument, so SFINAE needs to be applied to either the return type or extra function arguments. SFINAE is a much more powerful solution, it can be used not only to provide
superandextendsbut with many other features of types or compile time values. Google for SFINAE and you will find many cases of SFINAE being used, many of them will be C++03 style.There was a proposal for concepts that would have greatly simplified the syntax, but no agreement was reached and in a move to push the standard to completion it was deferred for a later standard.
Now, this is really not that common in C++ as it is in Java, so I recommend that you provide a different question with what you want to do, and you will get ideas for designs in more idiomatic C++.