ChatGPT解决这个技术问题 Extra ChatGPT

How do I explicitly instantiate a template function?

I have a template function with one argument. I have to instantiate that function without calling that function means explicitly I have to instantiate.

I have this function:

template <class T> int function_name(T a) {}

I instantiated that function like this:

template int function_name<int>(int);

But I got the following errors:

error: expected primary-expression before 'template'
error: expected `;' before 'template'

h
hrnt

[EDIT 2]: Note that there was some confusion regarding the code in the original question due to code formatting issues. See AnthonyHatchkins' answer for more details.

If you really want to instantiate (instead of specialize or something) the function, do this:

template <typename T> void func(T param) {} // definition

template void func<int>(int param); // explicit instantiation.

[EDIT] There seems to be (a lot) of confusion regarding explicit instantiation and specialization. The code I posted above deals with explicit instantiation. The syntax for specialization is different. Here is syntax for specialization:

template <typename T> void func(T param) {} // definition

template <> void func<int>(int param) {} // specialization

Note that angle brackets after template!


that is instantiation or specialization?
Not true. You can tell the compiler to explicitly instantiate templates. Google for "C++ explicit template instantiation" for more details.
@Nawaz: you are wrong. Of course it is always the compiler the one that instantiates, that line is a request from the programmer to the compiler to instantiate the template. If you have a copy of the C++ standard, read 14.7.2 Explicit instantiation
Specialization means you are probably changing its implementation. Instantiation simply means you are assigning it to a particular compilation unit, possibly to take its unique address or make it available as a library function or to reduce bloat.
@hrnt: I think, you're right. @Ashot : I just noticed that the syntax doesn't has template<> form. What he has written is different from specialization. +1 for teaching me this new thing. I'm removing my post. :D
A
Antony Hatchkins

Your code is correct.

The error message pertains to a place in the code that you didn't quote here.

Update:

Original code was

template <class T> int function_name(T a) {}
template int function_name<int>(int);

and it was correct.

But it was not quoted and thus looked like this:

template int function_name(T a) {}
template int function_name(int);

It generates the following error

a.cpp:1: error: explicit instantiation of non-template ‘int function_name’
a.cpp:1: error: expected `;' before ‘(’ token
a.cpp:3: error: ‘function_name’ is not a template function

which is clearly different from what OP cited.

In this variant the second line is ok (<int> can be omitted here), but the first line is faulty. The compiler cannot guess that T is a template parameter.


Technically it was not his code, it was Bill's edit :) The original code is template int function_name( T a) { } and template int function_name(int);
@hrnt Original code was not formatted correctly, yet it was correct. If I were Balaji I would return and accept your answer if he considers it useful, yet for me (and presumably for anyone else) your answer (while being perfectly right by itself) doesn't answer the question.
@hrnt You're right that the original code looked like that. But because of deduced template argument it still works. I have to agree with Antony Hatchkins that the error arises from code that the OP didn't quote, however I think your answer is still useful for people who didn't know about explicit instantiation.
Well, take me for example. I googled this page when I wanted to refresh my well-forgotten knowledge about explicit instantiation - and it didn't help me much. As for me, there is no confusion about the syntax. What may cause confusion about the explicit instantiation is its usage
@AntonyHatchkins Ah, true - I didn't look at the source of the original question, just how it appeared on my screen. I'll +1 this and amend my original answer to note the confusion regarding the original question.
G
Gelldur

This may be helpful for instantiation template method when we want to split cpp/hpp file.

// foo.hpp

struct Foo
{
    template<typename T>
    void myMethod(T var);
};

// foo.cpp
#include <typeinfo>
#include <iostream>

template void Foo::myMethod(int var);

template void Foo::myMethod(double var);

template <typename T>
void Foo::myMethod(T var)
{
    std::cout << typeid(T).name() << " - " << var << std::endl;
}

Example:

    Foo foo;
    foo.myMethod(1);
    foo.myMethod(2.0);
    
    // undefined reference to `void Foo::myMethod(float)'
    // foo.myMethod(2.0F); <-- doesn't work as we don't have definition
OUT:
i - 1
d - 2

You can play it here: https://onlinegdb.com/gwAjMF9QH