Created
October 22, 2015 09:28
-
-
Save artemkin/61d919c6da3fa2d63833 to your computer and use it in GitHub Desktop.
C++ Explicit instantiation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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