相关疑难解决方法(0)

从std :: string转换为bool

将std :: string转换为bool的最佳方法是什么?我正在调用一个返回"0"或"1"的函数,我需要一个干净的解决方案来将其转换为布尔值.

c++ string boolean

48
推荐指数
7
解决办法
8万
查看次数

为什么将bool隐式转换为字符串不是错误?

我瞪着它并试图在SO上找到类似的问题,但没有找到任何有用的东西.所以,在这里发布我的问题.

考虑这个程序:

#include <iostream>
void foo(const std::string &) {}
int main() 
{
    foo(false);
}
Run Code Online (Sandbox Code Playgroud)
[Warning] converting 'false' to pointer type for argument 1 of 'std::basic_string::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]' [-Wconversion-null]

为什么C++在没有显式强制转换的情 我原以为会遇到编译错误.由于以下异常显示,程序在运行时异常终止:

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct null not valid

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Run Code Online (Sandbox Code Playgroud)

关于这种隐式转换的标准是什么?

c++ string boolean language-lawyer implicit-conversion

6
推荐指数
1
解决办法
1194
查看次数