从泛型中获取bool值作为返回的对象,可以使用以下方法:
template<typename T>
class BoolWrapper {
public:
static bool getBool() {
return false;
}
};
template<>
class BoolWrapper<bool> {
public:
static bool getBool() {
return true;
}
};
#include <type_traits>
template<typename T>
bool getBool() {
return std::is_same<T, bool>::value;
}
#include <type_traits>
template<typename T>
typename std::enable_if<!std::is_same<T, bool>::value, bool>::type
getBool() {
return false;
}
template<typename T>
typename std::enable_if<std::is_same<T, bool>::value, bool>::type
getBool() {
return true;
}
以上方法都可以用于从泛型中获取bool值作为返回的对象。具体使用哪种方法,需要根据实际情况进行选择。
领取专属 10元无门槛券
手把手带您无忧上云