Problem
The following is a simplified, contrived example of an issue that I'm facing. Essentially, I need an object that can hold an arbitrary number of items, and return those items when needed.
template<typename... Ts>
class Foo {
public:
Foo(Ts... args) :
mArgs{std::forward<Ts>(args)} {
}
std::tuple<Ts...> getArgs() const {
return mArgs;
}
private:
std::tuple<Ts...> mArgs;
};
This works fine when args > 1.
Foo<int, int> f{1, 2};
auto result = f.getArgs(); // result is a tuple containing two ints.
However, if args == 1, I would prefer to NOT get a tuple from getArgs.
Foo<int> f{1};
auto result = f.getArgs(); // result is a tuple, but I want it to be an int.
Question
Is there a way, perhaps using SFINAE, to define another getArgs function that is used when args == 1? Something like (and this is obviously very wrong):
template<typename = std::enable_if_t<std::tuple_size_v<Ts...> == 1>>
??? getArgs() const {
return mArgs;
}
Several obvious problems with this:
- I'm not certain if
std::tuple_size_v<Ts...>will even work. - I don't know what the new return type will be. Perhaps
std::tuple_elementwould be useful here.