Reputation: 4997
I'd like to distinguish between empty and non-empty tuples, and I came up with the following solution (example):
#include <tuple>
#include <iostream>
template <typename ... Args>
void function(const std::tuple<Args...>& t)
{
std::cout << "empty tuple" << std::endl;
}
template <typename Head, typename ... Args>
void function(const std::tuple<Head, Args...>& t)
{
std::cout << "tuple of size: " << sizeof...(Args) + 1 << std::endl;
}
int main()
{
function(std::make_tuple()); // picks 1st function
function(std::make_tuple(1)); // picks 2nd function
function(std::make_tuple(1, 2, 3, '4')); // picks 2nd function
}
However, using variadic Args
to match std::tuple<>
is misleading for a reader, and I think that introducing Head
in the 2nd overload is excessive. Is there a simple way to write an overload that matches std::tuple<>
directly?
Upvotes: 2
Views: 138
Reputation: 17704
Did you try:
void function(const std::tuple<>& t) { ... }
?
Then you don't need to write Head
out separately in the second function. Live example: http://coliru.stacked-crooked.com/a/1806c3a8a3e6b2d1.
Upvotes: 3