warning C4661:no suitable definition provided for explicit template instantiation request

Viewed 6135

I wrote a class template and use it in different DLLs, so wish to hide some parts of the implementation.

To do this, I use "template instantiation", but export it, like this, here is the header file:

#include <iostream>
#include <exception>

using namespace std;

template<typename T>
class __declspec(dllexport) Templated
{
    public:
        Templated();
};

template __declspec(dllexport) Templated<int>;

int main()
{
   cout << "Hello World" << endl; 
}

And the definition is in a separate file (.cpp)

template<typename T>
Templated<T>::Templated() {}

template Templated<int>;

My problem is that I got a warning, even if the instantiation is marked as exported!

You can test this code here : http://webcompiler.cloudapp.net/, it will generate the C4661 warning!

Is this normal?

2 Answers

Maybe other will tackle this issue like me. I found workaround for this (for some may be unacceptable though).

You can just create class and export it:

class __declspec(dllexport) TemplatedInt : public Templated<int> {};

No warnings, works fine

Related