news 2026/7/11 3:25:42

C++编程实践—false_type和true_type的实践应用

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++编程实践—false_type和true_type的实践应用

一、说明

c++标准中提供了元编程接口std::integral_constant,而在这个接口中又提供了针对bool类型的两个实例化类型:

true_type std::integral_constant<bool,true>false_type std::integral_constant<bool,false>

在前面的文章分析中,特别是针对std::index_sequence的相关文章中,对其进行过介绍。另外在分析其它模板和元编程的过程中,也对它们进行过顺便的应用说明。但对它们的应用并没有系统性的说明。本文将针对其实际应用的方式进行整体的总结说明。

二、应用形式

在模板或元编程中,true_type和false_type基本有两种应用形式:

  1. 用作基类
    这种是最典型的情况,比如前面的成员检测:
template<typename T,typename=void>structcheckSize:std::false_type{};template<typename T>structcheckSize<T,std::void_t<decltype(std::declval<T>().size())>>:std::true_type{};

而在元编程库中的很多接口都继承自它们,比如std::is_pointer等:

template<class T>structis_pointer:std::false_type{};template<class T>structis_pointer<T*>:std::true_type{};template<class T>structis_pointer<T*const>:std::true_type{};template<class T>structis_pointer<T*volatile>:std::true_type{};template<class T>structis_pointer<T*constvolatile>:std::true_type{};

在实际的开发中,这种情况也是非常常见的。

  1. 标签分发处理
    这种情况主要有函数重载处理、静态断言和条件编译:
#include<iostream>#include<type_traits>#include<utility>structCopyType{};structMoveType{MoveType()=default;MoveType(constMoveType&)=delete;MoveType(MoveType&&)=default;};template<typename T>voidtestFunc(T value,std::true_type){std::cout<<"call copy!"<<std::endl;}template<typename T>voidtestFunc(T value,std::false_type){std::cout<<"call move!"<<std::endl;}template<typename T>voidprocess(T value){testFunc(std::forward<T>(value),std::is_copy_constructible<T>{});}intmain(){CopyType ct;process(ct);MoveType nct;process(std::move(nct));return0;}

其它的实现也都类似,不再过多列举。

三、实现方法

std::false_type和std::true_type是从std::integral_constant扩展而来的,所以需要看integral_constant具体的实现:

template<class T,T v>structintegral_constant{staticconstexpr T value=v;using value_type=T;using type=integral_constant;// using injected-class-nameconstexpr operatorvalue_type()constnoexcept{returnvalue;}constexpr value_typeoperator()()constnoexcept{returnvalue;}// since c++14};template<bool B>using bool_constant=integral_constant<bool,B>;using true_type=integral_constant<bool,true>;using false_type=integral_constant<bool,false>;

大家可以尝试着用不同的方式来实现并进行工程验证。

四、例程

下面给出一个例程供大家分析:

#include<iostream>#include<type_traits>// 正确转换函数template<typename F,typename T>voidcastType(constF&from,T&to,std::true_type){to=static_cast<T>(from);std::cout<<"cast ok!"<<std::endl;}//错误处理函数template<typename F,typename T>voidcastType(constF&from,T&to,std::false_type){static_assert(std::is_convertible<F,T>::value,"convertible result");std::cout<<"cast error"<<std::endl;}template<typename F,typename T>voidcastCheckRun(constF&from,T&to){castType(from,to,std::is_convertible<F,T>{});}intmain(){intd=1;doubledd=0.0;std::string str="abc";castCheckRun(d,dd);//castCheckRun(d, str); //errorreturn0;}

再看一下std::is_function的源码实现:

template<class>structis_function:std::false_type{};// specialization for regular functionstemplate<class Ret,class...Args>structis_function<Ret(Args...)>:std::true_type{};// specialization for variadic functions such as std::printftemplate<class Ret,class...Args>structis_function<Ret(Args......)>:std::true_type{};// specialization for function types that have cv-qualifierstemplate<class Ret,class...Args>structis_function<Ret(Args...)const>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)volatile>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)constvolatile>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)const>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)volatile>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)constvolatile>:std::true_type{};// specialization for function types that have ref-qualifierstemplate<class Ret,class...Args>structis_function<Ret(Args...)&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)const&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)volatile&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)constvolatile&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)const&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)volatile&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)constvolatile&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)&&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)const&&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)volatile&&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)constvolatile&&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)&&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)const&&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)volatile&&>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)constvolatile&&>:std::true_type{};// specializations for noexcept versions of all the above (C++17 and later)template<class Ret,class...Args>structis_function<Ret(Args...)noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)constnoexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)volatilenoexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)constvolatilenoexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)constnoexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)volatilenoexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)constvolatilenoexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)const&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)volatile&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)constvolatile&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)const&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)volatile&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)constvolatile&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)&&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)const&&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)volatile&&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args...)constvolatile&&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)&&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)const&&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)volatile&&noexcept>:std::true_type{};template<class Ret,class...Args>structis_function<Ret(Args......)constvolatile&&noexcept>:std::true_type{};

std::is_function处理的原理基本是函数类型(对象)不能有cv限定符且不能绑定到非const的左值引用(可绑定到const的左传引用或右值引用)。当然实际应用时一般是使用函数在地址转换时会隐式的转为函数指针。不过看上面的代码可以发现,其实为了编译处理更简单,就是靠量大管饱,大量特化相关的函数判断即可。
也可以这样理解,通过模板偏特化(partial specialization)列出可能的全部函数类型签名形式,对合法的函数类型进行std::true_type特化;其它由非函数类型则由匹配主模板,特化为 std::false_type。

五、总结

元编程和模板编程的复杂是大家公认的,但只要大家从基础的知识点出发,将不同的基础知识灵活运用起来,再加上多看网上开源的好的工程代码,就能够较快的掌握元编程和模板编程的技术。与诸君共勉!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/5 4:39:35

如何快速掌握C4编译器:面向新手的终极指南

如何快速掌握C4编译器&#xff1a;面向新手的终极指南 【免费下载链接】c4 x86 JIT compiler in 86 lines 项目地址: https://gitcode.com/gh_mirrors/c42/c4 C4编译器是一个极简主义的杰作&#xff0c;它用仅仅四个函数就实现了完整的C语言编译功能。这个开源项目不仅展…

作者头像 李华
网站建设 2026/7/4 14:05:35

高效微信管理神器:WeChatTweak-macOS防撤回与多开功能完全指南

高效微信管理神器&#xff1a;WeChatTweak-macOS防撤回与多开功能完全指南 【免费下载链接】WeChatTweak-macOS A dynamic library tweak for WeChat macOS - 首款微信 macOS 客户端撤回拦截与多开 &#x1f528; 项目地址: https://gitcode.com/gh_mirrors/we/WeChatTweak-m…

作者头像 李华
网站建设 2026/7/8 6:39:55

Ink/Stitch免费刺绣设计软件完整使用指南

Ink/Stitch免费刺绣设计软件完整使用指南 【免费下载链接】inkstitch Ink/Stitch: an Inkscape extension for machine embroidery design 项目地址: https://gitcode.com/gh_mirrors/in/inkstitch 厌倦了昂贵的专业刺绣软件&#xff1f;想要一个真正免费且功能强大的设…

作者头像 李华
网站建设 2026/7/3 22:31:34

Simditor多语言解决方案:构建全球化富文本编辑器的技术实践

Simditor多语言解决方案&#xff1a;构建全球化富文本编辑器的技术实践 【免费下载链接】simditor An Easy and Fast WYSIWYG Editor 项目地址: https://gitcode.com/gh_mirrors/si/simditor 在数字内容创作日益全球化的今天&#xff0c;富文本编辑器作为内容生产的核心…

作者头像 李华
网站建设 2026/6/29 9:34:48

Python版本管理终极指南:告别版本冲突,拥抱高效开发

Python版本管理终极指南&#xff1a;告别版本冲突&#xff0c;拥抱高效开发 【免费下载链接】pyenv Simple Python version management 项目地址: https://gitcode.com/GitHub_Trending/py/pyenv 你是否曾经遇到过这样的情况&#xff1a;新项目需要Python 3.11的最新特性…

作者头像 李华
网站建设 2026/7/4 17:28:02

HestiaCP服务器管理7大典型问题深度解析与实战修复

HestiaCP服务器管理7大典型问题深度解析与实战修复 【免费下载链接】hestiacp Hestia Control Panel | A lightweight and powerful control panel for the modern web. 项目地址: https://gitcode.com/gh_mirrors/he/hestiacp 作为一款轻量级且功能强大的现代Web服务器…

作者头像 李华