fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #define TEXTIFY(s) (s)
  5.  
  6. int main()
  7. {
  8. std::string text1 = R"(begin
  9. a,
  10. b,
  11. c
  12. end)";
  13. std::cout << "text1[BGN]" << std::endl;
  14. std::cout << text1 << std::endl;
  15. std::cout << "text1[END]" << std::endl;
  16.  
  17. // 通常の文字列リテラルで同じ内容を表現
  18. std::string text2 = "begin\na,\nb,\nc\nend";
  19. std::cout << std::boolalpha << "equality 1 2: " << (text1 == text2) << std::endl;
  20.  
  21. // プリプロセッサを通す場合(1)
  22. std::string text3 = TEXTIFY(text1);
  23. std::cout << std::boolalpha << "equality 2 3: " << (text2 == text3) << std::endl;
  24.  
  25. // プリプロセッサを通す場合(2)
  26. std::string text4 = TEXTIFY(R"(begin
  27. a,
  28. b,
  29. c
  30. end)");
  31. std::cout << std::boolalpha << "equality 2 4: " << (text2 == text4) << std::endl;
  32.  
  33. // プリプロセッサを通す場合(3)
  34. // マクロ定義が複数行に渡る場合、本来は \ で行継続せねばならないが、RAW stringの行継続は特別視
  35. #define TEXT_DEFINITION R"(begin
  36. a,
  37. b,
  38. c
  39. end)"
  40. std::string text5 = TEXT_DEFINITION;
  41. std::cout << text5 << std::endl;
  42. std::cout << std::boolalpha << "equality 2 5: " << (text2 == text5) << std::endl;
  43. }
  44.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
text1[BGN]
begin
a,
b,
c
end
text1[END]
equality 1 2: true
equality 2 3: true
equality 2 4: true
begin
a,
b,
c
end
equality 2 5: true