7

Can a C++ user-defined literal operator ever be passed a null pointer?

This is really happening with an experimental version of g++ (gcc version 4.7.0 20111114 (experimental) [trunk revision 181364] (Debian 20111114-1)) but I am unsure if this is a bug (90% sure) or some strange expected behavior.

Example program:

#include <iostream>
#include <stdexcept>
#include <string>

std::string operator "" _example (const char * text) {
  using std::cerr;
  using std::endl;
  cerr << "text (pointer) = " << static_cast<const void *>(text) << endl;
  if (!text) throw std::runtime_error("Is a null pointer really expected?");
  cerr << "text (string) = \"" << text << '"' << endl;
  return text;
}

int main() {
  2_example;
  1_example;
  0_example;
}

Output (probably a bug in gcc ... but maybe not?!, hence the question):

text (pointer) = 0x8048d49
text (string) = "2"
text (pointer) = 0x8048d4b
text (string) = "1"
text (pointer) = 0
terminate called after throwing an instance of 'std::runtime_error'
  what():  Is a null pointer really expected?
Aborted

It's not just "0_example"; it's whenever the literal value is zero. For example, it still happens even when the literal is "0x0000_example".

Is this a bug? Or some strange special-case when the literal's value is zero?

wjl
  • 7,519
  • 2
  • 32
  • 41
  • 10% unsure == 90% sure, so you are 90% sure it is a bug and 90% sure it is expected behaviour. – Seth Carnegie Dec 05 '11 at 00:39
  • 1
    @Seth heh, I guess that is how confused this behavior has made me. ;) It took almost a half-hour to figure out what was failing when I ran into this behavior in a much more complicated real problem where I was adding user-defined literal support. I suppose I should fix the wording. =) – wjl Dec 05 '11 at 00:42
  • 1
    ¤ That's a compiler bug. C++11 §2.14.8/3 "the *literal L* is treated as a call of the form `operator "" X ("n")`". No exception to that rule (when it applies) is defined. Cheers & hth., – Cheers and hth. - Alf Dec 05 '11 at 00:48
  • @Alf that was my feeling as well, but I'm not (yet) a guru on C++11 standards, and I figured it was *possible* I missed something. If you make your comment with the standard reference an answer I'll mark it accepted (and go file a gcc bug if it's not listed already). – wjl Dec 05 '11 at 00:52
  • Certainly `0_example` should not call the literal operator with a null pointer, that much is obvious. In fact, any invocation through a literal expression can necessarily not pass a null pointer. However, you can always call the operator directly as an ordinary function, in which case you can pass it anything you like, even a null pointer. – Kerrek SB Dec 05 '11 at 00:53
  • @wjl: just accept my comment :-), and go file a bug... Cheers, – Cheers and hth. - Alf Dec 05 '11 at 00:56

1 Answers1

5

As Alf P. Steinbach assured me in a nice comment, this is a bug, not standard behavior (no big deal, since I was using a gcc snapshot).

I went to file a gcc bug, but it looks like it's already been filed and fixed upstream:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50958

wjl
  • 7,519
  • 2
  • 32
  • 41