5

Clang 3.1 claims to support user defined literals. I can define this:

int operator"" _tryit(long double n) { return int(n); }

but when I try to use it I get an error:

int m = 5_tryit;

Invalid suffix '_tryit' on integer constant

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
John
  • 2,326
  • 1
  • 19
  • 25
  • 4
    How about `int m = 5.0_tryit;`? – ildjarn Jun 04 '12 at 23:22
  • 2
    Apologies if I'm insulting your intelligence, but… that looks like the error I get when I compile in C++03 mode instead of C++11 mode, or when I use clang-3.0 instead of 3.1, not like any of the errors I get when I misuse a suffix in 3.1's C++11 mode. Are you sure you're using the right version and -std flag? – abarnert Jun 05 '12 at 00:31

1 Answers1

7

5 cannot be implicitly converted to a long double in your case. You need to change it to 5.0 to make it a long double or explicitly invoke the function yourself for the implicit conversion to work:

int m = 5.0_tryit;

OR

int n = operator"" _tryit(5);

(tested both with clang version 3.1 (trunk) (llvm/trunk 155821))

This SO question has a good explanation of the rules.

(Also, as abarnert mentions, make sure you are passing the -std=c++11 flag to the compiler when compiling).

Community
  • 1
  • 1
Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • @John: I just tested `int m = 5.0_tryit;` with clang 3.1 and it worked for me. – Jesse Good Jun 04 '12 at 23:51
  • 3
    Isn't there some versioning disconnect between what comes from Clang and what Apple ships? I.e., I don't think what Apple calls "Clang 3.1" is the same as what Clang calls "Clang 3.1". C.f. the comments on [this answer](http://stackoverflow.com/a/10603994/636019). – ildjarn Jun 04 '12 at 23:54
  • Apple's Xcode 4.3.2 version is not quite the same as you get with llvm.org's official llvm-3.1 version, and of course neither one is the same as the llvm/trunk 155821 listed in Jesse Good's answer above. But both the Xcode 4.3.2 version and the MacPorts clang-3.1 version seem to have the same behavior as what Jesse Good posted, so the differences apparently aren't relevant here. – abarnert Jun 05 '12 at 00:28
  • I appreciate the help. Has anyone tested "5.0_tryit" with Xcode 4.4 (developer preview)'s version of what it claims is Clang 3.1? – John Jun 05 '12 at 13:18
  • @John: Did you pass the compiler flag (-std=c++11)? – Jesse Good Jun 05 '12 at 20:28