Something like print END << END; in C++?

后端 未结 8 1743

Is there anyway to do something like PHP\'s

print << END
yadayadayada
END;

in C++? (multi-line, unescaped, easy-to-cut-and-paste stre

8条回答
  •  臣服心动
    2021-01-02 00:12

    A slash at the end of a line means the next line is a continuation of the string. This leads you to an alternate syntax that you may or may not prefer over other suggestions:

    std::cout << "\
    This is a\n\
    multiline\n\
    string.\
    ";
    

    The advantages: you don't have to wrap each line in quotes; when importing text from some other source you only have to edit one end of each line, not both ends (more macro friendly); and you can see exactly how the resulting string will look, including leading spaces.

    The disadvantages: you still have to explicitly include \n for newlines, and I don't like losing the ability to indent my code nice and pretty. HEREDOC syntax also suffers from this feature, so perhaps that won't bother you.

    Personally, this isn't a syntax I like.

提交回复
热议问题