Use of function template name with no prior declaration in function call with explicit template arguments is a c++20 extension

Explanation of using function template name with no prior declaration in function call with explicit template arguments in C++20

C++20 introduced an extension that allows using a function template name in a function call without prior declaration, as long as explicit template arguments are provided.

Traditionally, when calling a function template, the template arguments need to be explicitly stated unless the template can be deduced from the function arguments. However, with this new extension, it is possible to provide the template arguments directly without needing a prior declaration of the template.

Let’s take a look at an example to understand this better:

    
      #include <iostream>

      template <typename T>
      void print(T value) {
          std::cout << value << std::endl;
      }

      int main() {
          print<int>(10);  // using function template name without prior declaration
          return 0;
      }
    
  

In the above example, we have a function template named print which takes in a single parameter and prints it. In the main function, we call the print function with the template argument int without any prior declaration of the template. This is possible due to the C++20 extension.

Note that this extension only works when explicit template arguments are provided. If template arguments cannot be explicitly stated, the traditional way of declaring the template explicitly or relying on template deduction still needs to be followed.

Read more interesting post

Leave a comment