This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [C++-11] User defined literals

On 09/20/2011 04:15 PM, Jason Merrill wrote:
On 09/19/2011 10:07 PM, Ed Smith-Rowland wrote:
On 09/19/2011 05:11 PM, Jason Merrill wrote:
Can't you store the string and the suffix, and then interpret the
number later if you end up calling an operator that takes the value?

I could and I thought abut it but from the wording (2.14.8 p3 and p4) it
looks like the numeric argument gets first try, then raw literal then
literal template. I thought I might as well let the preprocessor do the
numeric conversion since I'll always check it first.

Fair enough. We could do the overload resolution before we produce the actual value to be passed, but I guess that's probably not a sufficient optmization to bother with.

+       error ("user-defined literal operator template %q+D"
+              " conflicts with user-defined raw literal operator %q+D",
+              newdecl, olddecl);

You should only use + for one of the decls; that sets the source position for the diagnostic.

+ TK(CHAR_USERDEF, LITERAL) /* 'char'_suffix - C++-0x */ \
+ TK(WCHAR_USERDEF, LITERAL) /* L'char'_suffix - C++-0x */ \
+ TK(CHAR16_USERDEF, LITERAL) /* u'char'_suffix - C++-0x */ \
+ TK(CHAR32_USERDEF, LITERAL) /* U'char'_suffix - C++-0x */ \
+ TK(STRING_USERDEF, LITERAL) /* "string"_suffix - C++-0x */ \
+ TK(WSTRING_USERDEF, LITERAL) /* L"string"_suffix - C++-0x */ \
+ TK(STRING16_USERDEF, LITERAL) /* u"string"_suffix - C++-0x */ \
+ TK(STRING32_USERDEF, LITERAL) /* U"string"_suffix - C++-0x */ \
+ TK(UTF8STRING_USERDEF,LITERAL) /* u8"string"_suffix - C++-0x */ \

Could we avoid adding all these additional codes by setting a flag on the token? It seems odd to have extra codes for char/string literals but not for numbers.

+       const char *suffix;
+       cpp_get_userdef_suffix (tok->val.str, '\'', &suffix);

Let's return the suffix pointer instead of passing in the address of a local variable. And pass in the token pointer rather than the string and a delimiter; libcpp can figure out which delimiter to use based on the token type. It could also handle the flag I suggested above so that the front end doesn't need to know about it.


Here is another iteration of the patch.

I tried getting rid of all the codes.
1. I couldn't even bootstrap the result.
2. I sort of like the codes - these really are different sort of tokens - strings with suffixes.
3. One could argue that then I should have a CPP_NUMBER_USERDEF token. I'm mixed. On one hand this would be more consistent. On the other, suffixed numbers are an old tried and true token. This last is why the treatment of numbers was different from strings and chars.

I finally figured out template operators. They work now.

One thing doesn't work. Earlier I had said that friend declarations failed. Not true. What fails is defining the function body in the class definition. The function is simply not recorded. I'm trying to track this down but pointers would be helpful. If I write the function body outside the class definition friend works perfectly.

About friends. The lookup for user-defined literal operators isn't ADL. They are a lot like normal functions because of the suffix-id as opposed to operator<< in this regard. Thus a friend literal operator should be accessible both as an explicit operator call and via a literal. Both are tested and except as noted above about inline defs the tests pass.

I have some repeated error messages I would like to clean up. I want the missing underscore warning to appear only once. Again, pointers would be helpful.

Basically, the thing works.

Thank you,


Attachment: patch25
Description: Text document

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]