Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Template type deduction C++

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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>();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading