Skip to content

Instantly share code, notes, and snippets.

@herczy
Created September 7, 2011 21:24
Show Gist options
  • Select an option

  • Save herczy/1201782 to your computer and use it in GitHub Desktop.

Select an option

Save herczy/1201782 to your computer and use it in GitHub Desktop.
Template specialization problem (outline)
// This is the original template
template < typename _Type >
class SomeClass
{
public:
// Some functions
void foo()
{
}
void bar(int a)
{
}
// etc.
// This is what we want to specialize
void specialize_me()
{
do_something();
}
};
// BAD: We have to copy all functions even if they are the same
template <>
class SomeClass< SomeType >
{
public:
// Some functions
void foo()
{
}
void bar(int a)
{
}
// etc.
// This is what we want to specialize
void specialize_me()
{
do_something_else();
}
};
// This would be ideal, but it doesn't compile with MSCV
template <>
SomeClass< SomeType >::specialize_me()
{
do_something_else();
}
// So we solve it like this
template < typename _Type >
void outside_specialize_me()
{
do_something();
}
template <>
void outside_specialize_me()
{
do_something_else();
}
template < typename _Type >
class SomeClass
{
public:
// Some functions
void foo()
{
}
void bar(int a)
{
}
// etc.
// This is what we want to specialize
void specialize_me()
{
outside_specialize_me< _Type >();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment