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;
}

 

posted @ 2017-09-22 09:56  略加思索的河马  阅读(174)  评论(0编辑  收藏  举报