3

I tried invoking a method directly on the result of the user-defined literal, but my results varied depending on the compiler.

#include <string>

std::string operator ""_test(unsigned long long value)
{
    return std::to_string(value);
}

int main()
{
    1000_test.size();   // 1) NOK on Clang/GCC, OK on MSVC
    (1000_test).size(); // 2) OK everywhere
    1000_test + "a";    // 3) OK everywhere
}

Demo on Godbolt

Case 1) seems odd, since the compilers seem to disallow defining _test.size user-defined literal.

So which compiler is correct here for the 1) case? What does the standard say?

sthlm58
  • 1,142
  • 1
  • 9
  • 28
  • @T.C. The duplicate doesn't quite answer this question. The question is why MSVC compiles? I tested it and although intellisense shows errors, the code compiles and executes delivering correct results. This brings the question, why can't you follow with `.` operator immediately after literal for integer? Understandable for floating point numbers, they have decimal points, but integers do not have them. Any ideas? – Killzone Kid Feb 25 '18 at 00:41
  • @Killzone - The nasty details are in the last comment on the dupe. As the literal starts with a digit, it is a number - and numbers can contain `.`. And all kinds of letters too, if it is a hexadecimal floating point. That's as far as the preprocessor knows when tokenizing. (And MSVC's preprocessor is known to be far from up-to-date). – Bo Persson Feb 25 '18 at 07:30
  • @BoPersson I suspected something like that might be the reason, thanks you. – Killzone Kid Feb 25 '18 at 07:46

0 Answers0