相关疑难解决方法(0)

从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
查看次数

标签 统计

c++ ×1

g++ ×1

g++4.9 ×1