将true / false分配给std :: string:发生了什么事? [英] assigning true/false to std::string: what's going on?

查看:147
本文介绍了将true / false分配给std :: string:发生了什么事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在源代码上测试了一个c ++ 11编译器,它在我的一个函数中发现了一个错误,我希望我的非c ++ 11编译器能够抓住它。我从一个返回类型为std :: string的函数返回false ...这是显示问题的代码

  #include< iostream> 

int main()
{
std :: string str = false;

std :: cerr<< '<< str<< '<<的std :: ENDL;

返回0;
}


$ g ++ test.cpp -W -Wall -Wextra
$ ./a.out

在投掷后终止'std :: logic_error'的实例
what():basic_string :: _ S_construct NULL无效
中止

我很惊讶这段代码编译没有问题。我怀疑从异常描述中,编译器将false转换为0,然后转换为NULL,并将其用作char *来尝试构造字符串..



但是,当我将false切换为true时,这里是我得到的: b $ b test.cpp:函数'int main()':
test.cpp:5:错误:从'bool'转换为非标量类型'std :: string'请求

这是一个比较合理的结果,在我看来。



Can有人请澄清为什么这种看似不一致的行为发生?也就是说, std :: string a = false 编译但引发异常, std :: string a = true 不会编译。



编辑:



std = c ++ 11 for false case:

  test.cpp:在函数'int main()'中:
test.cpp:5:23:warning:将'std :: basic_string< _CharT,_Traits,_Alloc> :: basic_string(const _CharT *,const _Alloc&)的参数1转换为指针类型[with _CharT = char; _Traits = std :: char_traits< char>; _Alloc = std :: allocator< char>]'[-Wconversion-null]

不过,正如CashCow所建议的那样

解决方案

这是一个可怕的隐式转换和缺乏类型安全性。

std :: string 从一个指针
的构造函数中减去0,变成一个空指针。



,并且不能将空指针传递给std :: string的构造函数。

顺便说一句,当你使用=它是一个构造函数而不是您在这里执行的任务。



您的严格g ++ C ++ 11编译器很好地在编译时抓住了您的错误。



它不能用于true,因为它永远不能表示NULL指针。 C ++ 11有nullptr。如果您尝试:

std :: string str = nullptr;



你的C ++ 11编译器可能会编译它,然后你会得到一个运行时错误。


I was testing a c++11 compiler on my source code and it caught an error in one of my functions that I would have expected my non c++11 compiler to catch as well. I was returning false from a function that has a return type of std::string... Here's the code that demonstrates the problem

#include <iostream>

int main ( )
{
    std::string str = false;

    std::cerr << "'" << str << "'" << std::endl;

    return 0;
}


$ g++ test.cpp -W -Wall -Wextra
$ ./a.out

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

I'm very surprised that this code compiles with no problems. I suspect from the exception description is that the compiler is converting a false to 0 and then to NULL and uses that as a char * to try and construct the string..

However, when I switch false to true, here's what I get:

$ g++ test.cpp -W -Wall -Wextra
test.cpp: In function ‘int main()’:
test.cpp:5: error: conversion from ‘bool’ to non-scalar type ‘std::string’ requested

That's a more reasonable result, in my opinion.

Can someone please clarify why this seemingly inconsistent behaviour happens? That is, std::string a = false compiles, but throws an exception, and std::string a = true doesn't compile.

EDIT:

For reference, here's an error generated with g++ 4.7 with -std=c++11 for the false case:

test.cpp: In function ‘int main()’:
test.cpp:5:23: warning: converting ‘false’ to pointer type for argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ [-Wconversion-null]

It does accept NULL though as CashCow suggests

解决方案

It's rather a horrible implicit conversion and lack of type-safety.

std::string takes a constructor from a pointer false degrades to 0 which becomes a null pointer.

and you cannot pass a null pointer to the constructor of std::string.

Incidentally whilst you use = it is a constructor not an assignment you are performing here.

Your "strict" g++ C++11 compiler however nicely caught the error for you at compile time.

And it won't work with true because that is never able to represent a NULL pointer. C++11 has nullptr. If you tried:

std::string str = nullptr;

your C++11 compiler would probably compile it and then you'd get a runtime error.

这篇关于将true / false分配给std :: string:发生了什么事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆