I would like to create a template function that returns either int or std::vector<int> depending on a template parameter. For example:
struct ReturnInt {};
struct ReturnVec {};
[...]
int num = func<ReturnInt>();
std::vector<int> nums = func<ReturnVec>();
I’ve been attempting naively to implement this based on my very limited experience with TMP. I feel like it should involve something along the lines of explicit template specializations, std::enable_if, std::conditional, and/or SFINAE. But none of my attempts to code this will compile, let alone run in a simple test.
How would this return-type switching be implemented?
Edit: As noted in the comments, this is a simplification of my actual problem. If it helps, I have a class that accepts a template parameter. Depending on the parameter, I would like its get() method to return either a single object/value, or a standard container of objects/values.
Based on your edit, you just want a bog-standard template specialization
If you only want to modify one member function based on the template, but reuse the rest of the class definition, you can use a helper class