I am hoping to achieve such type matching function:
MyClass<int, bool> temp;
temp.print(-1, false); // works
temp.print(-1); // compile error, too less argument
temp.print(-1, "string"); // compile error, wrong type
temp.print(-1, false, 'c'); // compile error, too many arguments
Basically, when given template parameter, MyClass
's function print
accepts arguments of exactly the same type, no more, no less.
And this is my current implementation:
template<class... ClassType>
class MyClass {
public:
template<typename... ArgType>
void print(ArgType... args) {
matchType<ClassType...>(args...);
}
private:
template<typename Type>
void matchType(Type t) {
std::cout << "matching " << t << "\n";
std::cout << "end\n";
}
template<typename FirstType, typename... Rest>
void matchType(FirstType firstArg, Rest... args) {
std::cout << "matching " << firstArg << "\n";
matchType<Rest...>(args...);
}
};
But it fails to detect and match type that it compiles well for the code:
MyClass<int, bool> temp;
temp.print(-1, false); // works
temp.print(-1, "string"); // works, shouldn't work
temp.print(-1, false, 'c'); // works, shouldn't work
Can someone explain to me what have I done wrong?