0

I asked a question on Iterators here: Prefer Iterators Over Pointers? I've come to understand some of the protection and debug capabilities they offer as a result.

However, I believe that begin and end now offer similar possibilities on C-style array.

If I want to create a const string that will only be iterated over in STL algorithms, is there still an advantage to using a const string, or should I prefer const char[] with begin and end?

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 3
    If you are using C++ you should prefer `std::string` over`char[]` – NathanOliver Mar 20 '15 at 12:34
  • A quick clarification - from your view, is the additional std::string API (including the safer string::at(size_t) not that important? – Fox Mar 20 '15 at 12:35
  • 2
    `However, I believe that begin and end now offer similar possibilities on C-style array` - erm, the "iterators" there do **not** offer protection or debug capabilities – sehe Mar 20 '15 at 12:39
  • @NathanOliver the complication here is that even a `const std::string` cannot be constructed at compile time (yet) so the program **will** pay the price at start up/first seen when the string is constructed. – Mgetz Mar 20 '15 at 12:41
  • @Mgetz I thought C++ already had `std::string` literals. – juanchopanza Mar 20 '15 at 12:42
  • @juanchopanza it does, but they are still runtime literals. They are still constructed at runtime. The user defined literal syntax is just a convenience. Without `constexpr` the construction must happen during execution. – Mgetz Mar 20 '15 at 12:45

1 Answers1

2

So the answer depends on what version of c++ you're using

C++98

Because C++98 doesn't have std::begin or std::end the best move is just to accept you're going to have to pay the costs of construction and use std::string. If you have boost available you should still consider boost::string_ref for two reasons. First its construction will always avoid allocation, and is overall much simpler than std::string.

boost::string_ref works because it only stores a pointer to the string and the length. Thus the overhead is minimal in all cases.

c++11

Very similar to C++98 except the recommendation to use boost::string_ref becomes MUCH stronger because c++11 has constexpr which allows the compiler to bypass construction completely by constructing the object at compile time.

c++1z

Allegedly (it's not final) the Library Fundamentals TS will be bringing us std::string_view. boost::string_ref was a prototype for an earlier proposal of std::string_view and is designed to bring the functionality to all versions of C++ in some form.

On C++14 String Literals

C++14 introduced string literals with the syntax "foo"s unfortunately this is just a convenience. Because operator""s is not constexpr it cannot be evaluated at compile time and thus does not avoid the penalty that construction brings. So it can be used to make code nicer looking, but it doesn't provide any other benefit in this case.

Mgetz
  • 5,108
  • 2
  • 33
  • 51