+ 1

Why input type is restricted for user defined literals

Hello I know only specific type is accepted for user defined literals but return type can be anything... Why it is so? Is there any reason of not allowing other type as input parameter or is it just a standard on c++ to be followed ? Refer below code and try to change input as long double to double and you will observe the compile error. What's a big deal in double and long double? https://code.sololearn.com/c3e4KlDH9h97/?ref=app

15th Dec 2020, 12:58 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 odpowiedzi
+ 1
Well, the language is just designed that way. There are specific data types that are meant to be used as arguments for each literal data type, when creating user-defined literal suffixes. I think these specific data types help the compiler during overload resolution, to choose the correct function depending on the literal type being used. For example, Let's say I want to use the same user-defined literal suffix(d for example) for both ints and doubles. I can create the two functions like this: int operator""d(size_t arg); double operator""d(long double arg); Then depending on the particular literal type being used,the compiler will choose any one of the two e.g. 5d; //1st function will be used 5.5d; //2nd function will be used
15th Dec 2020, 5:08 PM
Anthony Maina
Anthony Maina - avatar
0
Sorry but I am not getting point... Do you mean to say that compiler could have designed to decide on overload resolution like functions in case of literals as well bit it's not designed that way....reason for same is just that that's how c++ is
16th Dec 2020, 8:09 PM
Ketan Lalcheta
Ketan Lalcheta - avatar