当我运行下面的测试程序时,我原本希望字符串文字匹配std::string
构造函数,而不是调用bool
版本。我想知道标准中是如何涵盖的,我当然不明白为什么会将其转换为bool
。
当我将bool
替换为[unsigned] int
时,它会按预期工作。
#include <iostream>
#include <string>
class Test
{
public:
Test(bool value)
{
std::cout << "BOOL constructor: " << value << std::endl;
}
Test(std::string value)
{
std::cout << "string constructor: " << value << std::endl;
}
};
int main()
{
Test t0(true);
Test t1(false);
Test t2(2);
Test t3("TEST1");
std::string ts = "string";
Test t4(ts);
}
BOOL constructor: 1
BOOL constructor: 0
BOOL constructor: 1
BOOL constructor: 1
string constructor: string
BOOL constructor: 1
BOOL constructor: 0
BOOL constructor: 2
string constructor: TEST1
string constructor: string