#include <iostream>
#include <string>
struct mystruct{
mystruct(std::string s){
std::cout<<__FUNCTION__ <<" String "<<s;
}
explicit mystruct(bool s) {
std::cout<<__FUNCTION__<<" Bool "<<s;
}
};
int main()
{
const char* c ="hello";
mystruct obj(c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
mystruct Bool 1
Run Code Online (Sandbox Code Playgroud)
const char*
隐式转换为bool
而不是std::string
,虽然构造函数需要explicit
类型?c++ explicit constructor-overloading overload-resolution implicit-conversion
我最近遇到了下面显示的代码片段,我原以为它是一个语法错误,但令我惊讶的是,代码产生了有效的输出.
#include <stdio.h>
int main(void) {
int x = 2;
switch(x) {
case 1: printf("1"); break;
do {
case 2: printf("2 "); break;
case 3: printf("3 "); break;
} while(++x < 4);
case 4: printf("4"); break;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)output: 2 4
编译器:GCC 6.3
我发现了一个类似的问题,但它并不能完全证明上述条件, 在C中混合'切换'和'同时'
谁能解释一下,