Author Topic: C++ String Improvements  (Read 2952 times)

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
C++ String Improvements
« on: August 25, 2018, 04:26:50 AM »
There have been some improvements to the C++ string syntax. In particular, you can now specify an actual std::string literal using operator ""s.

I just tried it out. Seems you need to include the std::string_literals namespace. It also requires C++14 (though the underlying operator feature it's built on was introduced in C++11). Here's a working example:
Code: cpp [Select]

#include <string>
#include <type_traits>

int main() {
  using namespace std::string_literals;

  auto s1 = "This is a C string constant";
  auto s2 = "This is a C++ std::string constant"s; // <- note the trailing "s"

  static_assert(std::is_same<const char*, decltype(s1)>::value, "Types should match");
  static_assert(std::is_same<std::string, decltype(s2)>::value, "Types should match");

  return 0;
}


In terms of benefits, it means you can finally append to a string literal without having to explicitly wrap it in a new std::string object:
Code: cpp [Select]

throw new std::runtime_error(std::string("A wrapped constant explaining the problem with ") + someStrOfInterest); // <- No more!


Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 2352
  • OPHD Lead Developer
    • LairWorks Entertainment
Re: C++ String Improvements
« Reply #1 on: August 25, 2018, 10:25:51 AM »
This is definitely an improvement though I've found that when working with inline strings starting with a std::string is able to automatically 'cast' the literal to a std::string:

Code: [Select]
void somefunc()
{
    std::string str = "Whatever";
    std::cout << str + ", whatever 2" << endl;
}

Not really a cast versus instantiation of a second std::string and initializing it with the string literal or invoking a specialized operator+()... I haven't looked into it much further than on the surface.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Re: C++ String Improvements
« Reply #2 on: August 25, 2018, 10:37:50 AM »
Exactly.

I suppose I should write out how the throw example can now be written:
Code: cpp [Select]

throw new std::runtime_error("A std::string constant explaining the problem with "s + someStrOfInterest);


As for your example, it could be written:
Code: cpp [Select]

void somefunc()
{
    std::cout << "Whatever"s + ", whatever 2" << std::endl;
}


Though really, when you have two string literals in a row, you'd probably just combine them.

Another pain about C++, is prefixing everything with std::. I'm now kind of used to it in most contexts, but I still find it a bit overly verbose and tedious for quick debug output in test programs. In particular, I often forget the std:: prefix on the endl;)

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 2352
  • OPHD Lead Developer
    • LairWorks Entertainment
Re: C++ String Improvements
« Reply #3 on: August 25, 2018, 10:55:17 AM »
Specifying namespaces I tend to do in header files with using directives in the code file itself. And even then sometimes I specify std:: anyway as a form of documentation. I just kinda got used to it. Been weird not having to specify namespaces in C#... though to be fair instead of something like std:: you end up with System.Windows.Forms.Whatever.ThingIActuallyNeed... which can also be mitigated with using directives... this is turning into circular logic so I'll leave it here. :D

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Re: C++ String Improvements
« Reply #4 on: August 25, 2018, 12:23:19 PM »
Still better than Java ;)

Sad to hear you're still using using. Namespace abuse is a hard habit to kick.



Side note, you can use using as a replacement for typdef. It has a much more intuitive syntax and ordering:
Code: cpp [Select]

// Note the reversal
typedef TypeName alias; // Older C way
using alias = TypeName; // Newer C++ way


Offline Arklon

  • Administrator
  • Hero Member
  • *****
  • Posts: 1269
Re: C++ String Improvements
« Reply #5 on: August 25, 2018, 12:43:56 PM »
Still better than Java ;)

Sad to hear you're still using using. Namespace abuse is a hard habit to kick.



Side note, you can use using as a replacement for typdef. It has a much more intuitive syntax and ordering:
Code: cpp [Select]

// Note the reversal
typedef TypeName alias; // Older C way
using alias = TypeName; // Newer C++ way


C++11 "using" aliases can be templated, too, unlike typedefs. But you can't specialize them because fuck you, we're gonna make you abuse structs for everything because this is template hell, and then make aliases of the typedefs in the specialized structs, including in the actual implementation of STL type_traits.

Another cool thing is user-defined literal operators. Useful if you have your own string implementation you want to use instead of std::string, or you can make literal operators that return a hash of the string or its length or whatever.
« Last Edit: August 25, 2018, 12:46:14 PM by Arklon »

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Re: C++ String Improvements
« Reply #6 on: August 25, 2018, 10:29:30 PM »
The use of structs in templates is admittedly a little obscure and hard to read for the uninitiated. I much prefer D's compile time evaluation, which keeps syntax in line with run time evaluation.

Offline Vagabond

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1015
Re: C++ String Improvements
« Reply #7 on: August 27, 2018, 06:26:02 PM »
I would consider using namespaces in a .cpp file as a preference/style. Using a namespace in a header file to me would be abuse since it would force any code including the header to include the using statement. It does seem to be the trend to not apply using on namespaces in .cpp files as well.

I really like how in C#, using statements only apply to the file you are working in (this might partially be not true  since you can use the partial keyword to define a class across multiple files. I would have to check if the using statements also transferred on this case?)

In c#, since programmers do not typically use fully qualified names, it allows being more expressive in what names are used for namespaces. In C++ since no one adds using statements for namespaces, namespace names must be abbreviated and terse or else it will bloat the code significantly.

Not that this has much to do with std::string.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Re: C++ String Improvements
« Reply #8 on: August 28, 2018, 02:51:50 AM »
This, right here. Exactly.

Curiously, it seems there are two competing module systems proposed for a future C++ standard:
Modules in VS 2015
Modules (Clang)

I would really love to see a proper module system in C++.