Why was the space character not chosen for C++14 digit separators?

前端 未结 7 1712
心在旅途
心在旅途 2020-12-02 19:40

As of C++14, thanks to n3781 (which in itself does not answer this question) we may write code like the following:

const int x = 1\'234; // one thousand two          


        
7条回答
  •  臣服心动
    2020-12-02 20:19

    From wiki, we have a nice example:

    auto floating_point_literal = 0.000'015'3;
    

    Here, we have the . operator and then if another operator would be to be met, my eyes would wait for something visible, like a comma or something, not a whitespace.

    So an apostrophe does much better here than a whitespace would do.

    With whitespaces it would be

    auto floating_point_literal = 0.000 015 3;
    

    which doesn't feel as right as the case with the apostrophes.


    In the same spirit of Albert Renshaw's answer, I think that the apostrophe is more clear than the space the Lightness Races in Orbit proposes.

    type a = 1'000'000'000'000'000'544'445'555;
    type a = 1 000 000 000 000 000 544 445 555;
    

    Space is used for many things, like the strings concatenation the OP mentions, unlike the apostrophe, which in this case makes it clear for someone that is used separating the digits.

    When the lines of code become many, I think that this will improve readability, but I doubt that is the reason they choose it.


    About the spaces, it might worth taking a look at this C question, which says:

    The language doesn't allow int i = 10 000; (an integer literal is one token, the intervening whitespace splits it into two tokens) but there's typically little to no expense incurred by expressing the initializer as an expression that is a calculation of literals:

    int i = 10 * 1000; /* ten thousand */

提交回复
热议问题