I am currently studying variadic templates, and as a small exercise to digest some of the stuff I had been reading about, I wrote a small function to output the names of all of its argument types:
#include "stdafx.h"
#include <iostream>
#include <string>
template<typename Next, typename... Rest>
std::string get_arg_types(Next next, Rest... rest)
{
return std::string(typeid(Next).name()) + "\n" + get_arg_types(rest...);
}
template<typename Last>
std::string get_arg_types(Last last)
{
return std::string(typeid(Last).name());
}
int main()
{
float f = 0;
double d = 0;
int i = 0;
std::cout << get_arg_types(f, d, i);
return 0;
}
To my surprise, this compiled using VC++ 12.0 and (seems to) work just fine.
I expected an error due to ambiguity between the overload when there is only one argument remaining, because I read that a template parameter pack can be empty/contain 0 arguments.
So my question is why does this work? How is the "potential ambiguity" resolved? Are the signature for both functions not identical with only 1 arg? I feel like I may have missed some important concept somewhere, because in my head the above example shouldn't compile, but obviously I am wrong.
Kind regards :)