是否可以配置C++编译器以寻找另一个字符串文字标识符?

huangapple go评论60阅读模式
英文:

Is it possible to configure c++ compiler to look for another string literal identifier?

问题

现在是2023年,我认为将字符串的标识符设为在文本中如此常见的东西,而且在其他像标记语言等语言中也常见,这似乎有些愚蠢。

C++ 使用 `"` 符号来表示字符串的开始和结束。

有没有办法将这个符号设为其他的东西?

例如 `\xac`,它也是一个1字节的字符。

举个例子,如果字符串的标识符不是双引号,那么使用 `system()` 函数将会更容易,双引号也会变得非常有用。

PHP 也是一样的情况。我使用 echo `'<div class="adiv"></div>'`,以便我可以在双引号内写一些内容,但有时我需要同时使用单引号和双引号。

我们真的只能使用引号和双引号作为定界符吗?我们不能使用或设置一个不会出现在其他地方的自定义字符吗?

如果可以的话,请告诉我如何做。如果这只是一个幻想,请也告诉我。

//示例:(我知道我即将使用的字符是3字节的(\xe2\x81\x91),但这只是为了演示...

#include <iostream>

int main() {

  std::cout << ⁑Hello world!\n⁑;
  system(⁑echo -e '"hello\nworld"' | grep 'hello' | sed s/'"hello'/'"Hello!"'/⁑);

return 0;
}

我尝试在谷歌的许多页面中进行了搜索,甚至找不到任何人提出这个问题的人。
英文:

It's 2023 and I think it's silly that an identifier for a string is something that is found so commonly in text, and as well in other languages like markup.

C++ uses the " symbol to denote the beginning and end of a string.

Is there a way to have this symbol be something else?

For example \xac, which is also a 1-byte character.

As an example, if the string identifier were something other than a double-quote, then using the system() function would be a lot easier to use, where double-quotes would come in quite handy.

This goes for something like PHP as well. I use echo '<div class="adiv"></div>' with single-quotes so that I can write something inside with double-quotes, but sometimes I need to be able to use both double and single quotes.

Are we really limited to quotes and double-quote being our delimiters? Can't we use, or set, a custom character that doesn't appear elsewhere?

If it's doable, please tell me how. If it's a pipe dream, let me know as well.

//Example: (I know the character I'm about to use is 3-bytes (\xe2\x81\x91) but just for demonstration...

#include <iostream>

int main() {

  std::cout << ⁑Hello world!\n⁑;
  system(⁑echo -e '"hello\nworld"' | grep 'hello' | sed s/'"hello'/'"Hello!"'/⁑);

return 0;
}

I tried searching many pages (paginations?) of Google, and I can't even find anyone who asked the question.

答案1

得分: 4

如果我正确理解您的问题,这可以通过使用原始字符串文字轻松解决,例如:

std::string myStr = R"('&Using single quotes&, "double quotes")";

这些括号中的任何内容从(&)";将被视为字符串,您可以在该字符串内写任何内容,甚至是多行字符串而不需要\。请注意,像\n\r这样的符号也不会被视为'newline'或'return caret',而是按原样写入。

英文:

If I correctly understand your problem, this can be easy solved by using raw string literals, for example:

std::string myStr = R"('Using single quotes', "double quotes")";

Anything inside these brackets from "( to )" will be treated as string and you can write anything you want inside that string, even writing multiline string without \. Make note that symbols like \n, \r also will be treated not as 'newline' or 'return caret', but as they written.

huangapple
  • 本文由 发表于 2023年2月19日 07:47:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定