小编Dav*_*ius的帖子

从const字符串到bool的隐式转换

我有以下代码:

#include <iostream>
#include <string>

void foo(bool a)
{
        std::cout << "bool" << std::endl;
}

void foo(long long int a)
{
        std::cout << "long long int" << std::endl;
}

void foo(const std::string& a)
{
        std::cout << "string" << std::endl;
}

int main(int argc, char* args[])
{
        foo("1");
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

执行时我得到这个输出:

bool
Run Code Online (Sandbox Code Playgroud)

我原以为输出:

string
Run Code Online (Sandbox Code Playgroud)

为什么g ++ 4.9隐式将此字符串转换为bool?

c++ g++ g++4.9

5
推荐指数
1
解决办法
1320
查看次数

如何在取消引用nullpointer时创建错误?

以下代码编译时没有错误,并且运行时没有分段错误:

int* a = 0;
int& b = *a;
Run Code Online (Sandbox Code Playgroud)

我们在引用中确实有一个nullpointer.可以在项目中移动此引用一段时间,直到它被使用.当它被使用时,它将产生分段错误.

目前使用的编译器是MSVC 10.0和gcc 4.9.为了便于找到错误,我希望取消引用在nullpointers上失败,以便在上面的代码中引起分段错误.是否有可能实现这一目标以及如何实现?

c++ gcc visual-studio-2010

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

标签 统计

c++ ×2

g++ ×1

g++4.9 ×1

gcc ×1

visual-studio-2010 ×1