Why allow concatenation of string literals?

前端 未结 10 650
借酒劲吻你
借酒劲吻你 2020-11-30 14:47

I was recently bitten by a subtle bug.

char ** int2str = {
   \"zero\", // 0
   \"one\",  // 1
   \"two\"   // 2
   \"three\",// 3
   nullptr };

assert( int         


        
10条回答
  •  日久生厌
    2020-11-30 15:47

    I'm not sure about other programming languages, but for example C# doesn't allow you to do this (and I think this is a good thing). As far as I can tell, most of the examples that show why this is useful in C++ would still work if you could use some special operator for string concatenation:

    string someGlobalString = "very long " +
                              "so broken " +
                              "onto multiple " +
                              "lines"; 
    

    This may not be as comfortable, but it is certainly safer. In your motivating example, the code would be invalid unless you added either , to separate elements or + to concatenate strings...

提交回复
热议问题