Skip to content

[SUGGESTION] Multiple suffixes for literals, a revisit of Uniform Function Call Syntax #284

@msadeqhe

Description

@msadeqhe

In our life, every quantity can have multiple suffixes. For example:

  • 1 million dollar
  • 1 million euro
  • 20% of a bottle of water
  • 5,000 grams of apples
  • 1 hour of worker

In C++1, optional ' may be inserted between the digits of integer and floating-point literals as a separator. Therefore I propose that every suffix optionally may start with ' and mandatory ' must be inserted between them as a separator for multiple suffixes. Now, let's write the code for the above cases:

1'million'dollar
1'million'euro
20'percent'bottle'water
5'000'gram'apple
1'h'worker

Also built-in suffixes optionaly may start with '. They can be uppercase or lowercase. The built-in integer suffixes are L, LL, Z, U, UL, ULL and UZ (if U is provided, it must be the first in C++2, but it can be the last in C++1). The built-in floating-point suffixes are F, L, F16, F32, F64, F128 and BF16 as defined in C++23.

1'ULL
5'000.0'F

Encoding prefixes of character literals and string literals have to be changed to be suffix (similar to C# language), the first suffix have to be without ', because the end of character literals and string literals already have a quote:

'c'u8'symbol
"UTF-8 string view"u8'sv
R"(raw string)"u8's

As an extra feature, boolean literals may have suffixes, but it's really a special case for libraries:

false'news

In addition, we can write user defined suffixes after built-in suffixes:

100'UL'min
1.5'F'ms

Now, the question is what does it have common to uniform function call syntax. To answer this question, the following example is useful:

// In C++1
apple(gram(5'000))

// In C++2 with Uniform Function Call Syntax
// The above expression can be written as the following expression
5'000.gram().apple()

// In my suggestion, we can go further
5'000'gram'apple

So, definitely suffixes are the same as member functions or free functions. We don't need an operator"" to create user defined literals, each member function with the right signature can be used as suffix. Also free functions with the right signature can be used as suffix. For example:

Gram :type = {
    ...

    apple :(this) ->Apple = {
        ...
    }

    ...
}

gram :(value :int) ->Gram = {
    ...
}

x := 5'000'gram'apple;

No, suffixes should never get any extra parameter, it will ruin its purpose of being less verbose (being without parenthesis), and they should not modify this parameter.

Will your feature suggestion improve performance or expressiveness?

Yes, because user defined literals can improve expressivity of the programmer:

// store.sell(orange(box(2)));
a := store.sell(2'box'orange);

// price(water(bottle(2)) + juice(glass(0.5)))
b := price(2'bottle'water + 0.5'glass'juice);

Maybe it improves performance too, because for built-in suffixes the true type of the literal will be sent to the member function call:

hour :(value :float) ->Hour = {
	...
}

x := 1.0'F'hour;

If we rewrite the above example with operator"" in C++1, then the type of value parameter must be long double instead of float, because operator"" only accepts a parameter of type unsigned long long int or long double.

Uniform function call syntax will also improve generic programming for suffixes.

Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code?

No.

Will your feature suggestion automate or eliminate X% of current C++ guidance literature?

Yes. My feature suggestion will remove unnecessary extra concept operator"", and it will integrate its usage with normal member functions or free functions. Also It improves code readability.

The purpose of suffix notation is for units or quantities, member function notation is for object-style behaviors or states, and free function notation is for computing.

Describe why you've considered this feature request.

Currently I'm trying to suggest a new string literal for C++2. This suggestion will affect my suggestion about string literals. I hope to read your feedbacks here, therefore I can improve it or I should give up.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions