18

Possible Duplicate:
std::string length() and size() member functions

I always retrieved the length of an std::string via thesize() member function. To be honest I never knew there was a length() member function too. Well, I've just learnt there is. So I am wondering if there's any difference between the two, and in the likely event of a negative answer, why would there be two member functions that do exactly the same?

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434

1 Answers1

15

Nope! No difference. The more natural name for this function is length() and size() is provided alongside for uniformity with containers.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • @Armen: I don't have the book with me at the moment :) – Ry- Jun 28 '11 at 21:13
  • 4
    This would be problematic if in the future the semantics change. Semantically speaking(aside STL semantics), size can reflect the number of bytes of the string) while the length can refer to the number of "characters" of that string. In ASCII the two give the same result, but this doesn't hold for other encodings like UTF etc. – sitifensys Jun 28 '11 at 21:16
  • @Armen: One standard is ANSI/ISO C++, the other is something else... – Ry- Jun 28 '11 at 21:17
  • 2
    I think it has more to do with the fact that `size()` is defined in every STL container, but `length()` sounds more familiar for strings, as explained in the accepted answer of the [linked question](http://stackoverflow.com/questions/905479/stdstring-length-and-size-member-functions/905487#905487). I don't think that ISO and ANSI ever had different versions of the standard. Even the (I think original) SGI STL documentation states that `length` is just a "Synonym for `size()`." – Matteo Italia Jun 28 '11 at 21:19
  • 2
    @sitifensys: `length()` is defined as returning `size()`; there's no way that could be changed at this point in time, too much code relies on that behavior. – James McNellis Jun 28 '11 at 21:20
  • 3
    @sitifensys: doesn't really matter, since `std::basic_string` has no notion of "characters" as being any different from its `CharT` template parameter, and `CharT` is what it is (very close to being) a container of. So `size()` and `length()` both fundamentally must mean the size measured in units of `CharT`. While `size()` might be the size in bytes in some languages, distinct from the length in characters, it won't ever be distinct for `std::string`, and isn't for `std::wstring`. – Steve Jessop Jun 28 '11 at 21:23
  • @Matteo: No, it was never 2 different versions of ISO or ANSI. It was a different branch of C++. I don't remember the name. – Ry- Jun 28 '11 at 21:29
  • @James This is definitely true, and if it's defined as such in the standard, then there is no way to change it without breaking tons of code I guess :). @Steve I see the point, but I'm not talking about the implementation, but rather the meanings of `length` and `size` in the very particular case of strings. Correct me if I'm wrong but **strings** are used as **Strings of characters**, so the notion of characters **should** be included at some point in the future. – sitifensys Jun 28 '11 at 21:40
  • @sitifensys: specializations of `basic_string` are strings of characters, and the characters that they are strings of is whatever the `CharT` template parameter says. So, `std::string` is and always will be a string of `char`, and `std::wstring` of `wchar_t`. With suitable char traits you could have a `basic_string` for a string of Unicode code points on an implementation where `wchar_t` is too small. If you use any of those to store a higher-level variable-length encoding, that's your business. The standard library doesn't provide a class for variable-length encoded strings. – Steve Jessop Jun 29 '11 at 01:21
  • So, you might be right that `string` "should" know about variable-length encodings, but it doesn't. If the standard library ever provides a similar class that does, then it won't be called `string`, and it can define `size()` and `length()` as it pleases. – Steve Jessop Jun 29 '11 at 01:26