Skip to content

Instantly share code, notes, and snippets.

@hpcx82
Created July 23, 2011 06:52
Show Gist options
  • Save hpcx82/1101126 to your computer and use it in GitHub Desktop.
Save hpcx82/1101126 to your computer and use it in GitHub Desktop.
// Following class doesn't have to be templated, but we use template here to:
// 1. Make sure function's declaration and definition are in same place, so it is convenient when you update the function signature.
// Although inline could also achieve this, but you can't prevent others from moving implementation into a cpp, while template provide stronger protect.
// 2. Code will only be instantiated if used for template functions, thus reduce the binary size
// Although modern compiler and linker could remove non-used functions, that requires additional compiler or linker time to make it happen.
// But it also have some drawbacks
// 1. Compile error message is hard to read for templates
// 2. As all code are in header file, it is more vulnerable to #defines and typedefs when included
// Header file
template <typename E = void>
class ApplicationT
{
public:
bool Run()
{
//...
return true;
}
// ...
};
typedef ApplicationT<> Application;
// Source file
Application app;
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment