I try to write a IsLast
type traits to check if a given type is the last one in a std::tuple
, but the code below does not compile. I know how to get around it but I am curious why the compiler does not like it.I guess there must be some rule on specialization of variadic-template that I am not aware of.
The code is at: https://godbolt.org/g/nXdodx
Error message:
error: implicit instantiation of undefined template
'IsLast<std::tuple<std::__cxx11::basic_string<char>, int>, int>'
There is also a warning on specialization declaration:
warning: class template partial specialization contains template parameters that cannot be deduced; this partial specialization will never be used
#include <tuple>
#include <string>
/////////This works
template<typename TP, typename T>
struct IsFirst;
template<typename U, typename ...V, typename T>
struct IsFirst <std::tuple<U, V...>, T>
{
enum {value = false};
};
template<typename U, typename ...V>
struct IsFirst <std::tuple<U, V...>, U>
{
enum {value = true};
};
////////This doesn't compile
template<typename TP, typename T>
struct IsLast;
template<typename ...U, typename V, typename T>
struct IsLast <std::tuple<U..., V>, T>
{
enum {value = false};
};
template<typename ...U, typename V>
struct IsLast <std::tuple<U..., V>, V>
{
enum {value = true};
};
int main()
{
using T = std::tuple<std::string, int>;
bool v1 = IsFirst<T, std::string>::value;
bool v2 = IsLast <T, int>::value;
}