There have been some improvements to the C++ string syntax. In particular, you can now specify an actual std::string literal using operator ""s (https://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s).
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:
#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:
throw new std::runtime_error(std::string("A wrapped constant explaining the problem with ") + someStrOfInterest); // <- No more!
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:
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.
Exactly.
I suppose I should write out how the throw example can now be written:
throw new std::runtime_error("A std::string constant explaining the problem with "s + someStrOfInterest);
As for your example, it could be written:
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. ;)
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:
// Note the reversal
typedef TypeName alias; // Older C way
using alias = TypeName; // Newer C++ way