其他分享
首页 > 其他分享> > 类型别名和常量表达函数

类型别名和常量表达函数

作者:互联网

/*----------------------------------- 
      定义类型别名和常量表达函数 
 ------------------------------------*/ 

template<typename K, typename V>
class My_map {
{
    pair<typename add_const<K>::type ,V> default_node;
    // ...
};

定义类型别名:
template<typename T>
using Add_const = typename add_const<T>::type;

则上面的代码可改写为: 
template<typename K, typename V>
class My_map {
{
    pair<Add_const<K>,V> default_node;
    // ...
};
 
-------------------------------------------------------------- 

template<typename T>
void f(T& a)
{
    static_assert(std::is_floating_point<T>::value ,"FP type expected");
    // ...
}

定义常量表达函数:
template<typename T>
constexpr bool Is_floating_point<T>()
{
    return std::is_floating_point<T>::value;
} 

则上面的代码可改写为:
template<typename T>
void f(T& a)
{
    static_assert(Is_floating_point<T>(),"FP type expected");
    // ...
} 

 

标签:...,常量,point,别名,template,floating,type,函数
来源: https://www.cnblogs.com/lhb666aboluo/p/13339075.html