c++ - STL模板部分特例化。
天蓬老师
天蓬老师 2017-04-17 13:03:03
0
2
651
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
巴扎黑

C++ templates are generally used for generics (that is, one code is shared by multiple types), but they can also be used for non-type template parameters, which can be integers, characters, Boolean, etc.Equivalent types can be clearly determined through literals.

You usually declare a type template parameter as template <typename T>, but here the Non-type template parameter is template <bool B>. This is the only difference. If we want to talk about enable_if this template technique in detail, the key is not in Non-type or partial spec, but in SFINAE. You should learn this aspect.

Peter_Zhu

enable_if says template<bool _test>, so naturally you have to fill in true/false.

When using it, you will find that if the result of an expression expr is true, then enable_if<expr>::type exists. So you can write code like this:

template<typename T>
__forceinline T Copy(typename enable_if<sizeof(T)>=4, T>::type const& t)
{
    return t;
}

to test whether a type is no less than 4 bytes. If it is returned as is, a compilation error will occur if it is not.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!