c++ 模板和traits
#define TEST(ITEMNAME) AddItem(ITEMNAME, #ITEMNAME); template <typename T> void AddItem(T& var, const std::string& name) { cout << var << endl; cout << name << endl; } template <typename T> void foo_impl(T val, true_type) { cout << "one" << endl; } template <typename T> void foo_impl(T val, false_type) { cout << "two" << endl; } template <typename T> void foo(T val) { foo_impl(val, std::is_integral<T>()); } int main(int argc, char *argv[]) { int a = 100; TEST(a); foo(100.01); getchar(); return 0; }