c++用户定义的文字操作符可以传递空指针吗?
Can a C++ user-defined literal operator ever be passed a null pointer?
c++用户定义的文字操作符可以传递空指针吗?
这确实发生在g++的实验版本(gcc version 4.7.0 20111114 (experimental) [trunk revision 181364] (Debian 20111114-1)),但我不确定这是一个bug(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中的错误…但也许不是?,因此问题):
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";当字面值为0时。例如,即使字面值为"0x0000_example",它仍然会发生。
这是一个bug吗?或者一些奇怪的特殊情况,当文字的值为零?
正如Alf p . Steinbach在一个很好的评论中向我保证的那样,这是一个错误,不是标准行为(没什么大不了的,因为我使用的是gcc快照)。
我去提交一个gcc bug,但看起来它已经被归档并修复了:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50958相关文章:
- 通过函数指针定义类范围之外的方法
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 运行时错误:引用绑定到类型为"int"的空指针
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 为什么我在空指针错误(链表)中获取成员访问权限
- 从向量到空指针的 memcpy(反之亦然)不起作用
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- 成员访问是否在空指针上定义C++?
- 尝试将对象插入空指针数组时出现分段错误
- MPI_Op_create:候选功能不可行.自定义结构指针不能解释为空指针
- 指向自定义类型的静态指针在初始化后使用相同类型的静态非空指针保持 nullptr
- 正在递增定义良好的空指针
- 返回空指针的用户定义运算符new
- 通过空指针获取成员变量的地址是否会产生未定义的行为
- 在C++中删除一个空指针被认为是未定义的行为
- c++用户定义的文字操作符可以传递空指针吗?
- c++断言中的未定义行为:访问无效/空指针
- 为什么解引用空指针是未定义的行为
- 正在打印空指针未定义的行为
- 抛出自定义空指针异常