4

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;

I need to have a class that behaves as CustomString, but with the length function changed.

Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
  • 1
    There is no such thing as an `STL::basic_string`. Perhaps you mean `std::basic_string`? – Nicol Bolas Jul 01 '11 at 11:47
  • @Nicol Colas, I forgot to change it, it is alias for std – Mircea Ispas Jul 01 '11 at 11:48
  • 2
    @Nicol Bolas - I can see why you'd be tempted to make such an alias, but ... ewww! That's nearly as confusing as using `begin` and `end` instead of `{` and `}` and macroing `:=` to `=` to make your code look like Pascal. – T.E.D. Jul 01 '11 at 11:51

1 Answers1

7

You need to specialise the std::char_traits structure and override its static size_t length(const char_type* s); function to do that.

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;
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214