std::string::operator+(int) ?

The Partridge Family were neither partridges nor a family. Discuss.
binbinhfr
Posts: 78
Joined: May 9th, 2019, 10:57 pm

std::string::operator+(int) ?

Post by binbinhfr » March 29th, 2020, 5:13 pm

Hi,

I always use std::to_string() to concatenate a std::string to a number, but I wonder if, in C++, I could somehow add an operator+ that would concatenate a string and a number (integer).

I cannot add a new member operator+(int) to the predefined string class, right ?
But is there another way to achieve my goal that is to "lighten" the lisibility of string messages that I concatenate...

I noticed that there is a std::string::operator+(char)
I wonder why they did not go further ?

albinopapa
Posts: 4155
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: std::string::operator+(int) ?

Post by albinopapa » March 30th, 2020, 12:52 am

Make a non-member function overload:

Code: Select all

std::string operator+( std::string const& str, int num )
{
     return str + std::to_string( num );
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4155
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: std::string::operator+(int) ?

Post by albinopapa » March 30th, 2020, 12:56 am

Just wondering about your use of "lisibility", do you mean "legibility", "usability" or "visibility" or something else?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

binbinhfr
Posts: 78
Joined: May 9th, 2019, 10:57 pm

Re: std::string::operator+(int) ?

Post by binbinhfr » March 30th, 2020, 9:26 pm

sorry for my approximate english ;-)
By lisibility, I mean readability = make lines of code easy to read, with not too much cryptic stuff ;-)

Thx that was almost exactly what I was looking for, I did not know that you could make non-member overload !

but this line still gives an error on the second string "suffix":
s = "prefix" + id + "suffix";

I tried to overload
std::string operator+(int num, std::string const& str);

but it does not solve the problem....

By the way, do you know why they implemented the string+char, and not the string+int or string+double ?

albinopapa
Posts: 4155
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: std::string::operator+(int) ?

Post by albinopapa » March 31st, 2020, 4:09 am

First the operator+( char ), is adding a character to the end of a string, not a number.

Make sure to define both:
std::string operator+( int, std::string );
std::string operator+( std::string, int );

This way if the "int" comes before or after the std::string, it will call one of these functions.

This won't work for string literals, for those you'll have to call it like:
s = std::string( "prefix" ) + id + std::string( "suffix " );
or
s = ( std::string( "prefix" ) + id ) + "suffix";
or
s = "prefix" + ( id + std::string( "suffix" ) );
The second works with "suffix" as a string literal because the first operation in parentheses returns a std::string and operator+(std::string, char const* ) is defined.
The third one works for a similar reason, the first operation would be the parentheses and returns an std::string and operator+( char const*, std::string ) is defined.

This of course assumes you have defined the two other overloads.

It seems like a hassle, but a lot of people just make their own String class that wraps an std::string to add missing functionality.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4155
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: std::string::operator+(int) ?

Post by albinopapa » March 31st, 2020, 4:11 am

In regards to the lisibility, I finally looked it up and found that it does mean readability, I just couldn't figure it out based on context for some reason.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

binbinhfr
Posts: 78
Joined: May 9th, 2019, 10:57 pm

Re: std::string::operator+(int) ?

Post by binbinhfr » March 31st, 2020, 5:40 am

albinopapa wrote:
March 31st, 2020, 4:11 am
In regards to the lisibility, I finally looked it up and found that it does mean readability, I just couldn't figure it out based on context for some reason.
"lisibility" was just a direct (bad) translation of the french word "lisibilité". Sometimes, my vocabulary is missing... ;-)

for the string operator, I understand, but then how can I make it also work for string literal ?

I would like to write simple things like :

Code: Select all

vari = 12
vard = 52.5
s = "aaa " + vari + " bbb " + vard + " ccc " + 18.23 + " ddd " + 366
that will give "aaa 12 bbb 52.5 ccc 18.23 ddd 366"

is it possible that as long as the left operand (s) is a string, everything else of the right is converted to string ?

albinopapa
Posts: 4155
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: std::string::operator+(int) ?

Post by albinopapa » March 31st, 2020, 6:10 am

Apparently not. The only way to overload the operators is by having the parameter list include a user-defined type, such as std::string.

So operator+( char const*, int ) won't work. The closest thing you can do is use User Defined Literals.

There is one for std::string already built into the standard library:

Code: Select all

#include <string>

void func()
{
	// the user-defined literal operator""s() is defined in the std::literals namespace
	using namespace std::literals;
	auto str = "Here is a string literal converted using User Defined Literals"s;
}
The str here ends up being deduced as an std::string. Unfortunately, the is about as close as you can get, once you overload the operator+( std::string, int ) and operator+(int, std::string) then you can have:

Code: Select all

vari = 12;
vard = 52.5;
s = "aaa "s + vari + " bbb "s + vard + " ccc "s + 18.23 + " ddd "s + 366;
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4155
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: std::string::operator+(int) ?

Post by albinopapa » March 31st, 2020, 6:11 am

I guess you'd also have to overload the operator+ for double and float or whatever other types you want to concatenate strings with.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

binbinhfr
Posts: 78
Joined: May 9th, 2019, 10:57 pm

Re: std::string::operator+(int) ?

Post by binbinhfr » March 31st, 2020, 7:04 am

Well ok, there is no obvious solution.
I will just make a simple #define TOS(x) std::_to_string(x) to shorten the length of the stuff...

Post Reply