代码如下:‘
template<typename T>
struct has_no_destroy{
template<typename C>
static char test(decltype(&C::no_destroy));
template<typename C>
static int32_t test(...);
const static bool value = sizeof(test<T>(0)) == 1;
};
struct A{
};
struct B{
void no_destroy(){
}
};
struct C{
int no_destroy;
};
struct D:B{
};
void test(){
std::cout<<has_no_destroy<A>::value<<std::endl;
std::cout<<has_no_destroy<B>::value<<std::endl;
std::cout<<has_no_destroy<C>::value<<std::endl;
std::cout<<has_no_destroy<D>::value<<std::endl;
}“”“
我只想知道为什么使用test(0)而不是test(1),如果我运行第一个,结果是好的!但是第二个没有达到我的预期!有人能帮上忙吗?非常感谢!
发布于 2020-12-08 12:30:06
因为0可以被解释为空指针,这可以被重载的test接受。虽然1是一个int,只能被第二个重载的test接受,但这意味着value将始终为false。
https://stackoverflow.com/questions/65193038
复制相似问题