Skip to content

Instantly share code, notes, and snippets.

@artemkin
Created October 22, 2015 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artemkin/61d919c6da3fa2d63833 to your computer and use it in GitHub Desktop.
Save artemkin/61d919c6da3fa2d63833 to your computer and use it in GitHub Desktop.
C++ Explicit instantiation
// foo.h
#pragma once
template<typename A>
class Foo
{
public:
Foo();
void DoSomething();
template<typename B>
void DoSomethingElse(B b);
};
// foo.cpp
#include <iostream>
#include "a.h"
template<typename A> Foo<A>::Foo()
{
std::cout << "constructor" << std::endl;
}
template<typename A> void Foo<A>::DoSomething()
{
std::cout << "do something" << std::endl;
}
template<typename A>
template<typename B>
void Foo<A>::DoSomethingElse(B b)
{
std::cout << "size of " << sizeof(b) << std::endl;
}
// explicit instantiation of the class
template class Foo<int>;
// explicit instantiation of the method with A = int and B = double
template void Foo<int>::DoSomethingElse<double>(double);
// main.cpp
#include "foo.h"
int main()
{
Foo<int> foo;
foo.DoSomething();
foo.DoSomethingElse<double>(3.3);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment