c++字符特性模板char_traits,到底是什么?
admin
2024-03-13 06:00:11

c++字符特性模板,到底是什么?
作为一个老程序员,也一直对它觉得很是模糊和神秘,这到底是个什么东西,需要揭开这个面纱了.
怎么揭开,相关文档看不到,就剩下看代码了. 我的环境是ubuntu20, c++库是libstdc++-9-dev

打开/usr/include/c++/9/bits/char_traits.h, 这就是char_traits 的真身了.

它分为3个部分:
第一部分是 __gnu_cxx, 占据47-213行
namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
{
} // namespace

第2,3部分是 std 命名空间, 分成两段,共占据215-841行
namespace std _GLIBCXX_VISIBILITY(default)
{
}
就分析一下gnu 的吧,它代码比较少。说明问题为主,并最后给出一个测试.
它又分为声明部分,若干个函数实现部分,我就分析声明部分吧,它包含了几个简单的实现,
复杂的实现它放到了后面部分中,为简化把较复杂代码忽略了。
摘录下核心部分,75行,这部分代码可以和我的测试代码构成一个编译工程.

#include 
#include templatestruct _Char_types{//定义变量别名, 只不过它限定在_Char_types 结构中typedef unsigned long   int_type;typedef std::streampos  pos_type;typedef std::streamoff  off_type;typedef std::mbstate_t  state_type;};templatestruct char_traits{//再次重新定义变量别名,定义别名,只是为了使用时通俗化一点typedef _CharT                                    char_type;typedef typename _Char_types<_CharT>::int_type    int_type;typedef typename _Char_types<_CharT>::pos_type    pos_type;typedef typename _Char_types<_CharT>::off_type    off_type;typedef typename _Char_types<_CharT>::state_type  state_type;//static 是说,我这个函数是不需要this 指针的.//constexpr 是说, 函数是常量表达式, 在编译阶段就确定了函数形式//如果去掉char_traits 包装, 这跟普通的c 函数没有什么两样.static constexpr  voidassign(char_type& __c1, const char_type& __c2){ __c1 = __c2; }static constexpr booleq(const char_type& __c1, const char_type& __c2){ return __c1 == __c2; }static constexpr boollt(const char_type& __c1, const char_type& __c2){ return __c1 < __c2; }static constexpr  intcompare(const char_type* __s1, const char_type* __s2, std::size_t __n);static constexpr  std::size_tlength(const char_type* __s);static constexpr  const char_type*find(const char_type* __s, std::size_t __n, const char_type& __a);static constexpr char_type*move(char_type* __s1, const char_type* __s2, std::size_t __n);static constexpr char_type*copy(char_type* __s1, const char_type* __s2, std::size_t __n);static constexpr char_type*assign(char_type* __s, std::size_t __n, char_type __a);static constexpr char_typeto_char_type(const int_type& __c){ return static_cast(__c); }static constexpr int_typeto_int_type(const char_type& __c){ return static_cast(__c); }static constexpr booleq_int_type(const int_type& __c1, const int_type& __c2){ return __c1 == __c2; }static constexpr int_typeeof(){ return static_cast(_GLIBCXX_STDIO_EOF); }static constexpr int_typenot_eof(const int_type& __c){ return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }};

放上我的测试代码:

#include 
#include "char_traits.h"
int main()
{char c1='a';char c2='b';char_traits::assign(c1,c2); //类型charT给的是char, 所以参数就可以用char类型变量了printf("now c1 is:%c\n",c1);bool ret=char_traits::eq(c1,c2);printf("eq ret:%d\n",ret);
// 测试代码没有实现比较函数,如果想测试可以继续copy加入其对应实现部分
//	int iret=char_traits::compare((const char*)c1,(const char *)c2,sizeof(char));
//	printf("compare iret:%d\n",iret);return 0;
}

小结:
char_traits: 字符属性类. 处理的是字符类型
在它的命名空间下,实现了以下基本操作函数,
字符赋值,相等判别,小于判别,比较函数,长度,查找,移动,拷贝,类型变换,文件尾字符判别等。

如果忽略命名空间,再固定上变量类型,那它就是纯c 函数.
字符属性类也没有什么神秘性了,它是字符操作的一组函数,放到了char_traits 结构下.
你还可以扩展一些你想要的函数.

相关内容

热门资讯

德邦股份拟主动终止A股上市 将... 中访网数据  德邦物流股份有限公司于2026年1月13日召开第六届董事会第十三次会议,审议通过了关于...
沪锡收涨4.63% 转自:财联社【沪锡收涨4.63%】财联社1月14日电,国际铜夜盘收涨0.58%,沪铜收涨0.29%,...
我国成功发射卫星互联网低轨18... 北京时间2026年1月13日23时25分,我国在海南商业航天发射场使用长征八号甲运载火箭,成功将卫星...
0元就能在海外开店?警惕跨境电... (来源:南京晨报)转自:南京晨报 晨报讯(通讯员 江景轩 南京晨报/爱南京记者 刘通)做跨境...
鼓楼医院血管外科成功救治颈动脉... (来源:南京晨报)转自:南京晨报 近日,南京鼓楼医院血管外科成功为一名来自宁夏的张先生实施颈...