我注意到在我的SGI STL参考文献中,有一个关于角色特征的页面,但我看不出这些是如何使用的?他们是否替换了string.h函数?它们似乎没有被使用std::string,例如,length()方法on std::string不使用Character Traits length()方法.为什么角色特征存在并且它们是否曾在实践中使用过?
void assign(char_type& to, char_type from);
Run Code Online (Sandbox Code Playgroud)
为什么不能使用赋值运算符而不是使用此函数?这是用来做什么的?
目前 在 已经 问题在这里问#1 为什么 basic_fstream<uint8_t>不工作.答案说这char_traits只是专门用于char和wchar_t(加上char16_t,char32_t在C++ 11中),你应该坚持basic_fstream<char>阅读二进制数据并在需要时投射它.
好吧,这还不够好!:)
没有任何答案(我能找到)说明如何专门化char_traits<uint8_t>和使用basic_fstream模板,或者甚至可能.所以我想我会尝试自己实现它.
在Windows 7 64位上使用Visual Studio Express 2013 RC并在Kubuntu GNU/Linux 13.04 64位上使用g ++ - 4.7时,以下编译没有错误.但是它会在运行时抛出std :: bad_cast异常.我没有访问clang ++和libc ++来测试这个组合.
#include <cinttypes>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <locale>
#ifdef _WIN32
#define constexpr
#define NOEXCEPT throw()
#else
#define NOEXCEPT noexcept
#endif
// Change this to char and it works.
using byte_type …Run Code Online (Sandbox Code Playgroud)