I am getting an error qualified-id in declaration before ‘<’ token from the following code:
// g++ -std=c++20 example.cpp
#include <iostream>
template <typename U = int>
struct Example {
template <typename T>
static void execute() {
std::cout << "Hey" << std::endl;
}
};
int main() {
Example::execute<float>();
}
When I include the type for Example, such as Example<int>::execute<float>() it compiles successfully. Shouldn’t the compiler be able to deduce the type since I specified it as default value?
>Solution :
Class template argument deduction only applies when creating objects.
That is Example e; will deduce Example<int> e; via the default argument.
You are not creating an object though, and Example is not a class. You must include a template argument list. In this case, it can be empty though, since the template argument includes a default:
Example<>::execute<float>();