Zagorath
link
fedilink
English
321Y

Most languages support concatenation of strings using the + operator. The only mainstream languages I can think of that don’t are PHP (which uses “.”) and low-level languages like C & C++.

JavaScript might even concatenate some integers instead of adding them just for shits and giggles.

R uses paste0() for some reason

@meteorswarm@beehaw.org
link
fedilink
English
41Y

C++ does, but it’s not a very efficient operation. https://en.cppreference.com/w/cpp/string/basic_string/operator%2B

Zagorath
link
fedilink
English
2
edit-2
1Y

I ran

#include 
#include 

int main()
{
  std::string name;
  std::cout << "you"+"me";
}

Using cpp.sh, and got the following error:

main.cpp:7:21: error: invalid operands to binary expression ('const char[4]' and 'const char[3]')
  std::cout << "you"+"me";
               ~~~~~^~~~~
1 error generated.

edit: lemmy seems to be determined to convert my less than characters to their HTML entity codes, but the error is meant to point to the “+” sign.

@meteorswarm@beehaw.org
link
fedilink
English
11Y

This is because your operands are const char[]. That’s not a std::string.

I think your link has a double encoded % at the end: %25

The correct link is https://en.cppreference.com/w/cpp/string/basic_string/operator2B

Using the C++ standard library beyond the C backwards compatible parts? What devilry is this‽

Lua uses ..

Rikudou_Sage
link
fedilink
81Y

C++ does as well, doesn’t it? Though I don’t often use std::string, so I’m not sure. But every other string type I worked with had + overloaded.

Zagorath
link
fedilink
English
31Y

I dunno, I’ve never actually worked in C++, but I tried it out online and it didn’t seem to work.

Create a post

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
  • 1 user online
  • 77 users / day
  • 211 users / week
  • 413 users / month
  • 2.92K users / 6 months
  • 1 subscriber
  • 1.53K Posts
  • 33.8K Comments
  • Modlog