调试断言失败(无效空指针)默认字符串参数
Debug Assertion Failed (invalid null pointer) default string parameter
以下代码
void f(const std::string &s = {}) {
}
f();
在Visual Studio 2013中生成Debug Assertion Failed (invalid null pointer)
(调试版本)。它在gcc中运行正常(我可以将s.length()
设置为0)。当试图构建s
时,它看起来像一个错误:
下面是调用栈:
坐标系:f(); <-- HERE --
框架(xstring):
basic_string(const _Elem *_Ptr)
: _Mybase()
{ // construct from [_Ptr, <null>)
_Tidy();
assign(_Ptr); // <-- HERE --
}
框架(xstring):
_Myt& assign(const _Elem *_Ptr)
{ // assign [_Ptr, <null>)
_DEBUG_POINTER(_Ptr); <-- HERE --
return (assign(_Ptr, _Traits::length(_Ptr)));
}
框架(xutility):
template<class _Ty> inline
void _Debug_pointer(const _Ty *_First, _Dbfile_t _File, _Dbline_t _Line)
{ // test iterator for non-singularity, const pointers
if (_First == 0)
_DEBUG_ERROR2("invalid null pointer", _File, _Line); <-- HERE --
}
看起来是用(const char *)
调用构造函数而不是默认构造函数。
当我尝试这个时:
std::string x = {}; // local variable
调用默认构造函数
这是Visual Studio上的一个bug还是我错过了什么?
我知道一个解决办法是void f(const std::string &s = "")
,但我想使用默认初始化器,这是一个非常奇怪的行为。
这是一个bug。
如果它不是一个bug,那么你应该得到模棱两可的构造函数错误,因为被调用的构造函数不是默认构造函数,而默认构造函数可以被调用。
下面的代码可以在Visual c++ 12.0 (Visual Studio 2013附带的编译器)下编译:
struct X
{
X( char const* ) {}
};
void g( X const& = {} ) {}
不能编译
以下代码,其中构造函数参数不是默认可构造的,编译失败:
struct Y
{
struct E { E( int ){} };
Y( E ) {}
};
void h( Y const& = {} ) {}
具有枚举类型构造函数实参的中间情况可以编译,因此显然初始化式{}
不会转换为{0}
,而是转换为{{}}
–如果我可以推测,也许是为了抑制std::array
初始化的警告?
我已经向微软提交了一个bug报告。
相关文章:
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- Switch 语句(字符串)一直选择默认值,除非其为零
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- 如何比较C 中的两个字符串并使用默认功能
- 如何使用cont char启动字符串_View的默认值
- 当 n 的默认值为字符串长度时,如何反转字符串中的 n 个字符
- 在类实现中为字符串的覆盖默认破坏者
- 在不使用默认算法的情况下对字符串的 std 向量进行排序
- const字符串成员的错误:没有适当的默认构造函数可用
- C++默认构造函数:字符串参数与字符串参数()
- AnsiString作为Embarcadero C++Builder中字符串类型的默认值
- 默认参数声明:为什么默认字符串参数必须是 const
- 使用常量字符串时创建默认构造函数
- cpp-neltib(和 Boost)窗口中的默认字符串类型
- 字符串对的C++向量作为函数参数的默认规范在 gcc-4.1.2 上无效
- 将变量名作为字符串传递给具有默认参数的函数
- 字符串、列表、向量的默认构造函数的C++成本
- 在内联函数中使用字符串流作为默认值
- c++初始化动态内存中c样式字符串的默认值
- 调试断言失败(无效空指针)默认字符串参数