網(wǎng)站首頁 編程語言 正文
一、說明
Boost.EnableIf
Boost.Enable If 可以禁用重載函數(shù)模板或?qū)S妙惸0濉=靡馕吨幾g器忽略相應的模板。這有助于防止出現(xiàn)模棱兩可的情況,即編譯器不知道要使用哪個重載函數(shù)模板。它還可以更輕松地定義不僅可用于特定類型而且可用于一組類型的模板。
從 C++11 開始,Boost.EnableIf 已經(jīng)成為標準庫的一部分。您可以在不使用 Boost 庫的情況下調(diào)用本章介紹的函數(shù);只需包含頭文件 type_traits。
二、Boost.EnableIf的示例
示例 49.1。在返回值上使用 boost::enable_if 重載函數(shù)
#include <boost/utility/enable_if.hpp>
#include <type_traits>
#include <string>
#include <iostream>
template <typename T>
typename boost::enable_if<std::is_same<T, int>, T>::type create()
{
return 1;
}
template <typename T>
typename boost::enable_if<std::is_same<T, std::string>, T>::type create()
{
return "Boost";
}
int main()
{
std::cout << create<std::string>() << '\n';
}
Example49.1
示例 49.1 定義了函數(shù)模板 create(),它返回作為模板參數(shù)傳遞的類型的對象。該對象在 create() 中初始化,不接受任何參數(shù)。兩個 create() 函數(shù)的簽名沒有區(qū)別。在這方面,create() 不是重載函數(shù)。如果 Boost.EnableIf 沒有啟用一個函數(shù)而禁用另一個,編譯器將報告錯誤。
Boost.EnableIf 提供類 boost::enable_if,這是一個需要兩個參數(shù)的模板。第一個參數(shù)是條件。如果條件為真,第二個參數(shù)是 boost::enable_if 表達式的類型。訣竅在于,如果條件為假,則此類型不存在,在這種情況下,boost::enable_if 表達式是無效的 C++ 代碼。然而,當涉及到模板時,編譯器不會抱怨無效代碼。相反,它會忽略模板并搜索另一個可能適合的模板。這個概念被稱為 SFINAE,它代表“替換失敗不是錯誤”。
在示例 49.1 中,boost::enable_if 表達式中的兩個條件都使用類 std::is_same。此類在 C++11 標準庫中定義,允許您比較兩種類型。因為這樣的比較不是真就是假,所以使用 std::is_same 來定義條件就足夠了。
如果條件為真,相應的 create() 函數(shù)應返回作為模板參數(shù)傳遞給 create() 的類型的對象。這就是 T 作為第二個參數(shù)傳遞給 boost::enable_if 的原因。如果條件為真,則整個 boost::enable_if 表達式將替換為 T。在示例 49.1 中,編譯器會看到返回 int 的函數(shù)或返回 std::string 的函數(shù)。如果使用 int 或 std::string 以外的任何其他類型調(diào)用 create(),編譯器將報告錯誤。
示例 49.1 顯示提升。
示例 49.2。使用 boost::enable_if 為一組類型專門化函數(shù)
#include <boost/utility/enable_if.hpp>
#include <type_traits>
#include <iostream>
template <typename T>
void print(typename boost::enable_if<std::is_integral<T>, T>::type i)
{
std::cout << "Integral: " << i << '\n';
}
template <typename T>
void print(typename boost::enable_if<std::is_floating_point<T>, T>::type f)
{
std::cout << "Floating point: " << f << '\n';
}
int main()
{
print<short>(1);
print<long>(2);
print<double>(3.14);
}
Example49.2
示例 49.2 使用 boost::enable_if 為一組類型特化一個函數(shù)。該函數(shù)稱為 print() 并需要一個參數(shù)。它可以被重載,盡管重載要求您使用具體類型。要對一組類型(如 short、int 或 long)執(zhí)行相同的操作,您可以使用 boost::enable_if 定義適當?shù)臈l件。示例 49.2 使用 std::is_integral 來做到這一點。第二個 print() 函數(shù)為所有浮點數(shù)重載了 std::is_floating_point。
練習
使 print_has_post_increment() 寫入標準輸出,無論類型是否支持后增量運算符。例如,對于 int 程序應該輸出“int has a post increment operator”:
#include <string>
template <class T>
void print_has_post_increment()
{
// TODO: Implement this function.
}
int main()
{
print_has_post_increment<int>();
print_has_post_increment<long>();
print_has_post_increment<std::string>();
}
原文鏈接:https://yamagota.blog.csdn.net/article/details/127945003
相關(guān)推薦
- 2022-01-14 path.join()和path.resolve()區(qū)別
- 2021-12-05 CentOS7環(huán)境下gcc(版本10.2.0)升級詳細過程_Linux
- 2022-05-11 React中的Refs屬性你來了解嗎_React
- 2023-03-01 GoLang?Time時間操作函數(shù)講解_Golang
- 2023-03-15 pandas創(chuàng)建DataFrame對象失敗的解決方法_python
- 2022-04-18 Python腳本,標識符,變量使用,腳本語句,注釋,模塊引用詳解_python
- 2022-09-15 Python淺析迭代器Iterator的使用_python
- 2022-10-02 iOS實現(xiàn)可拖動的浮動菜單_IOS
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支