std::basic_string 特化 [英] std::basic_string specialization
问题描述
我只需要覆盖 std::basic_string 中的 length 函数,因为它对于特定平台上的自定义字符类型不正确.这是当前声明,比方说,CustomString
I need to override only length function from std::basic_string because it's not correct for a custom char type on a specific platform. This is current declaration for, let's say, CustomString
typedef STL::basic_string<CustomChar, STL::char_traits<CustomChar>, STL::allocator<CustomChar> > CustomString;
我需要一个类,其行为与 CustomString 相同,但长度函数已更改.
I need to have a class that behaves as CustomString, but with the length function changed.
推荐答案
你需要专门化std::char_traits
结构并覆盖它的 static size_t length(const char_type* s);
函数来做到这一点.
You need to specialise the std::char_traits
structure and override its static size_t length(const char_type* s);
function to do that.
那么在实例化basic_string
时甚至不需要指定所有模板参数.以下定义应该足够了:
Then you don’t even need to specify all the template parameters when instantiating a basic_string
. The following definition should be enough:
typedef std::basic_string<CustomChar> CustomString;
这篇关于std::basic_string 特化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!