lxg

导航

 
std::is_same,std::enable_if,std::is_integral
template<typename T>
bool isZero(T v)
{
    if (std::is_same<T, float>::value)
    {
        return (fabs(v) < FLT_EPSILON);
    }
    else  if (std::is_same<T, double>::value)
    {
        return (fabs(v) < DBL_EPSILON);
    }

    return v == 0;
}

template<typename T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type 
Foo1(T t)
{
    qDebug() << "Foo1: float";
    return t;
}

template<typename T>
typename std::enable_if<std::is_integral<T>::value, T>::type
Foo1(T t)
{
    qDebug() << "Fool: int";
    return t;
}
template<typename T>
typename std::enable_if<std::is_integral<T>::value, bool>::type
IsOdd(T i)
{
    return i % 2;
}

template<typename T, typename T1 = typename std::enable_if<std::is_integral<T>::value>::type>
bool IsEven(T i)
{
    return !bool(i % 2);
}
自定义c++模版函数
namespace cpp 
{
    template<class T, class U>
    struct is_same {
        static constexpr bool value = false;
    };

    template<class T>
    struct is_same<T, T> {
        static constexpr bool value = true;
    };
    template<class T, class U>
    constexpr bool is_same_v = is_same<T, U>::value;


    template<class T>
    struct integral_const {
        using type = T;
    };
    template<class T>
    struct remove_reference : integral_const<T> {};

    template<class T>
    struct remove_reference<T&> : integral_const<T> {};

    template<class T>
    using remove_reference_t = typename remove_reference<T>::type;
    template<class T>
    struct remove_const : integral_const<T> {};

    template<class T>
    struct remove_const<const T> : integral_const<T> {};

	template<class T>
	using remove_const_t = typename remove_const<T>::type;
}
conditional
namespace gld 
{
    // 若b为true,则定义T,若b为false,则定义F

    // 主模版
    template<bool, class T, class F>
    struct conditional {
        using type = T;
    };

    //利用模版特化,返回另一个类型
	template<class T, class F>
	struct conditional<false, T, F> {
		using type = F;
	};
    template<bool b, class T, class F>
    using conditional_t = typename conditional<b, T, F>::type;
}

struct ClassB {
    void print() {
        std::cout << "ClassB" << std::endl;
    }
};


struct ClassC {
	void print() {
		std::cout << "ClassC" << std::endl;
	}
};

template<bool b>
struct ClassA : public std::conditional_t<b, ClassB, ClassC>
{
};

    std::conditional<true, int, double>::type;
    std::conditional<false, int, double>::type;

    ClassA<true>().print();
    ClassA<false>().print();

	gld::conditional<true, int, double>::type;
    gld::conditional<false, int, double>::type;
工厂模版
template<typename T>
class GFactory
{
public:
    template<typename... Args>
    static T* Create(Args&&... args)
    {
        return new T(std::forward<Args>(args)...);
    }
};
模版别名

// 可以为模版类型参数提供默认值,但不能为函数模版参数通过默认值。然而,可以为非类型参数提供默认值
// 非类型模版参数 也就是没有typename或class

// 函数模版不支持偏特化,只有全特化

泛化、偏特化、全特化
// 模版类泛化和全特化
// 泛化
template<typename K, typename V>
struct MyMap
{
    MyMap()
    {
        qDebug() << "泛化版本K V";
    }
};

// 偏特化
template<typename V>
struct MyMap<std::string, V>
{
	MyMap()
	{
		qDebug() << "特化版本string V";
	}
};

// 全特化
template<>
struct MyMap<std::string, int>
{
	MyMap()
	{
		qDebug() << "特化版本string int";
	}
};

// 模版函数泛化和全特化
template<typename U, typename K>
auto Sum(U u, K k) -> decltype(u + k)
{
    return u + k;
}

template<>
std::string Sum<std::string, std::string>(std::string str1, std::string str2)
{
    return str1 + str2;
}

// 可变参数模版与 折叠表达式
template<typename... Args>
struct A2 {
A2(const Args& ... args) {
}
};

template<typename... Args>
int count(const Args&... args) {
return sizeof...(args);
}

template<typename... Args>
auto make_tuple(const Args&... args) {
return std::make_tuple(args...);
}

template<typename... Args>
auto make_array(const Args&... args) {
return std::array{args...};
}

posted on 2023-12-15 16:12  lxg_7105  阅读(11)  评论(0)    收藏  举报