Skip to content

Instantly share code, notes, and snippets.

@Zikoel
Created November 17, 2017 13:37
Show Gist options
  • Save Zikoel/c02e730a476bdef27d0e38ecb28997ca to your computer and use it in GitHub Desktop.
Save Zikoel/c02e730a476bdef27d0e38ecb28997ca to your computer and use it in GitHub Desktop.
Little exple of template specializations
#include <iostream>
struct Worker
{
template<typename TProcedure>
void execute(TProcedure p);
};
struct A
{
int fieldA = 1;
};
struct B
{
int fieldB = 2;
};
template<>
void Worker::execute<A>(A a)
{
std::cout << "Print value of A: " << a.fieldA << std::endl;
}
template<>
void Worker::execute<B>(B b)
{
std::cout << "Print value of B: " << b.fieldB << std::endl;
}
int main()
{
A a;
B b;
Worker w;
w.execute(a);
w.execute(b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment