Created
July 23, 2011 06:52
-
-
Save hpcx82/1101126 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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