C++11模板元編程-std::enable_if示例詳解
C++11中引入了std::enable_if函數(shù),函數(shù)原型如下:
template< bool B, class T = void > struct enable_if;
可能的函數(shù)實(shí)現(xiàn):
template<bool B, class T = void>
struct enable_if {};
template<class T>
struct enable_if<true, T> { typedef T type; };
由上可知,只有當(dāng)?shù)谝粋€(gè)模板參數(shù)為true時(shí),enable_if會(huì)包含一個(gè)type=T的公有成員,否則沒(méi)有該公有成員。
頭文件:
#include <type_traits>
std::enable_if使用場(chǎng)景
1、限制模板函數(shù)的參數(shù)類型
在某些場(chǎng)景下,我們需要實(shí)現(xiàn)只有特定類型可以調(diào)用的模板函數(shù)。如下代碼所示,通過(guò)對(duì)返回值使用std::enable_if和在模板參數(shù)中使用std::enable_if均實(shí)現(xiàn)了只允許整形參數(shù)調(diào)用函數(shù)的功能。
// enable_if example: two ways of using enable_if
#include <iostream>
#include <type_traits>
// 1. the return type (bool) is only valid if T is an integral type:
template <class T>
typename std::enable_if<std::is_integral<T>::value,bool>::type
is_odd (T i) {return bool(i%2);}
// 2. the second template argument is only valid if T is an integral type:
template < class T,
class = typename std::enable_if<std::is_integral<T>::value>::type>
bool is_even (T i) {return !bool(i%2);}
int main() {
short int i = 1; // code does not compile if type of i is not integral
std::cout << std::boolalpha;
std::cout << "i is odd: " << is_odd(i) << std::endl;
std::cout << "i is even: " << is_even(i) << std::endl;
return 0;
}
當(dāng)使用float類型參數(shù)調(diào)用函數(shù)時(shí),程序會(huì)報(bào)錯(cuò):
error: no matching function for call to 'is_odd(float&)'
2. 模板類型偏特化
在使用模板編程時(shí),可以利用std::enable_if的特性根據(jù)模板參數(shù)的不同特性進(jìn)行不同的類型選擇。
如下所示,我們可以實(shí)現(xiàn)一個(gè)檢測(cè)變量是否為智能指針的實(shí)現(xiàn):
#include <iostream>
#include <type_traits>
#include <memory>
template <typename T>
struct is_smart_pointer_helper : public std::false_type {};
template <typename T>
struct is_smart_pointer_helper<std::shared_ptr<T> > : public std::true_type {};
template <typename T>
struct is_smart_pointer_helper<std::unique_ptr<T> > : public std::true_type {};
template <typename T>
struct is_smart_pointer_helper<std::weak_ptr<T> > : public std::true_type {};
template <typename T>
struct is_smart_pointer : public is_smart_pointer_helper<typename std::remove_cv<T>::type> {};
template <typename T>
typename std::enable_if<is_smart_pointer<T>::value,void>::type check_smart_pointer(const T& t)
{
std::cout << "is smart pointer" << std::endl;
}
template <typename T>
typename std::enable_if<!is_smart_pointer<T>::value,void>::type check_smart_pointer(const T& t)
{
std::cout << "not smart pointer" << std::endl;
}
int main()
{
int* p(new int(2));
std::shared_ptr<int> pp(new int(2));
std::unique_ptr<int> upp(new int(4));
check_smart_pointer(p);
check_smart_pointer(pp);
check_smart_pointer(upp);
return 0;
}
程序輸出:
not smart pointer
is smart pointer
is smart pointer
參考材料
http://www.cplusplus.com/reference/type_traits/enable_if/
https://en.cppreference.com/w/cpp/types/enable_if
總結(jié)
到此這篇關(guān)于C++11模板元編程-std::enable_if的文章就介紹到這了,更多相關(guān)C++11模板元編程-std::enable_if內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++的static關(guān)鍵字及變量存儲(chǔ)位置總結(jié)
今天看博文時(shí),看到了c++的static關(guān)鍵字的一些總結(jié),還涉及到了一些代碼的存儲(chǔ)位置;接下來(lái)為您詳細(xì)呈現(xiàn)2012-11-11
C++11計(jì)時(shí)器之chrono庫(kù)簡(jiǎn)介
C++11有了chrono庫(kù),可以在不同系統(tǒng)中很容易的實(shí)現(xiàn)定時(shí)功能,要使用chrono庫(kù),需要#include,其所有實(shí)現(xiàn)均在std::chrono namespace下,本文給大家介紹C++11計(jì)時(shí)器:chrono庫(kù)介紹,感興趣的朋友一起看看吧2023-12-12
使用c++編程實(shí)現(xiàn)簡(jiǎn)單的打字小游戲
這篇文章主要為大家介紹了使用c++編程語(yǔ)言來(lái)實(shí)現(xiàn)一個(gè)非常簡(jiǎn)單的打字小游戲過(guò)程實(shí)現(xiàn)的示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之動(dòng)態(tài)分配實(shí)現(xiàn)串
這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之動(dòng)態(tài)分配實(shí)現(xiàn)串的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中動(dòng)態(tài)分配實(shí)現(xiàn)串的實(shí)例,需要的朋友可以參考下2017-10-10
VSCode配置Qt開(kāi)發(fā)環(huán)境的步驟
本文詳細(xì)介紹了在VSCode中配置Qt開(kāi)發(fā)環(huán)境的步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-09-09
C語(yǔ)言中數(shù)據(jù)結(jié)構(gòu)之鏈?zhǔn)交鶖?shù)排序
這篇文章主要介紹了C語(yǔ)言中數(shù)據(jù)結(jié)構(gòu)之鏈?zhǔn)交鶖?shù)排序的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09

