-3

I am very curious. What behavior does concatenating \b do to an std::string?

Will it simply add the '\b' to the end of the string, or do pop_back, and is this behavior defined? Although I can test it, I cannot do this experiment on every platform.

Some additional questions are if it just concatenated the backspace to the string, does this comparison return true or false?

std::string foo = "asdf\b";
std::string bar = "asd";
if(foo == bar)

Edit: I was pretty sure that it just concatenates the '\b' but I just wanted to be sure.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
FloweyTF
  • 323
  • 3
  • 11
  • 2
    The same behavior as concatenating any other character. One character is no different than any other character. "`\b`" is just a character, just like "`0`" or "`!`". – Sam Varshavchik Apr 19 '20 at 03:09
  • Escaped characters are characters like any other, there's no kind of run-time interpretation of character values. With ASCII encoding, appending `'\b'` to a `std::string` object just adds the `char` integer value `8` to the string. – Some programmer dude Apr 19 '20 at 03:11
  • I’m voting to close this question because it exhibits zero prior research. – user207421 Apr 19 '20 at 03:24
  • I did search through docs and stuff, and I was pretty sure that it would just concatenate the backspace. I just wanted to be sure. – FloweyTF Apr 19 '20 at 03:25
  • @user207421 That's only a suggetion, i'm not endeavoring to stop it. Ignore it if my guss is wrong :/ – con ko Apr 19 '20 at 03:32
  • I tried it on visual studio. The behavior was concatenation of backspace. However, I don’t currently have a easy way of getting Linux. – FloweyTF Apr 19 '20 at 03:32

1 Answers1

2

or do pop_back?

it won't, regardless which platform you are using.

does this comparison return true or false?

False.

because '\b' is also as an ASCII character as are all the others.

C++ won't make any guess on the function of the characters, for they are only integers, nothing special. The function of '\b' is processed by other programs, they don't remove any element in the string.

[cling]$ #include <string>
[cling]$ std::string a = "abcd"
(std::string &) "abcd"
[cling]$ std::string b = "abcde\b"
(std::string &) "abcde\x08"
[cling]$ a == b
(bool) false
con ko
  • 1,368
  • 5
  • 18
  • Yeah, I was pretty sure that was the answer. I just wanted to be sure. – FloweyTF Apr 19 '20 at 03:23
  • Re: "because `'\b'` is ... an ASCII character as are all the others" -- maybe; but there are systems that don't use ASCII, and nothing in this question depends on being ASCII. In C and C++ `'\b'` is a backspace character; the compiler produces a value for it that works as a backspace in whatever encoding the compiler is targeting. – Pete Becker Apr 19 '20 at 15:03