C用户定义的文字运算符是否可以传递空指针?

C用户定义的文字运算符是否可以传递空指针?

这实际上发生在g的实验版本(gcc版本4.7.0 20111114(实验性)[主干版本181364](Debian 20111114-1))但我不确定这是否是一个错误(90%肯定)或一些奇怪的预期行为.

示例程序:

#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;
}

输出(可能是gcc中的一个bug ……但也许不是?!,因此问题):

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

它不仅仅是“0_example”;每当字面值为零时都是如此.例如,即使文字是“0x0000_example”,它仍然会发生.

这是一个错误吗?或者一些奇怪的特例,当文字的值为零时?

解决方法

正如Alf P. Steinbach在一个很好的评论中向我保证的那样,这是一个错误,而不是标准行为(没什么大不了的,因为我使用的是gcc快照).

我去了一个gcc bug,但它看起来已经被归档并修复了上游:

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

相关文章

对象的传值与返回说起函数,就不免要谈谈函数的参数和返回值...
从实现装饰者模式中思考C++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...