正如@nm 在评论中所说,编译器不会停止编译 的错误分支,因此当目标类型为和目标类型为 时if
,两个分支都必须是有效代码。bool
VARIANT_BOOL
您可以通过分派到部分专门针对目标类型的帮助模板来避免该问题,例如
template<typename TDestination, typename TSource>
struct boolean_cast_impl; // undefined
template<typename TSource>
struct boolean_cast_impl<bool, TSource>
{
static bool cast(TSource source)
{
return source ? true : false;
}
};
template<typename TSource>
struct boolean_cast_impl<VARIANT_BOOL, TSource>
{
static VARIANT_BOOL cast(TSource source)
{
return source ? VARIANT_TRUE : VARIANT_FALSE;
}
};
template<typename TDestination, typename TSource>
TDestination boolean_cast(TSource source)
{
static_assert(std::is_same<TDestination, bool>::value || std::is_same<TDestination, VARIANT_BOOL>::value, "destination must be bool or VARIANT_BOOL");
return boolean_cast_impl<TDestination, TSource>::cast(source);
}
甚至只是在特化中定义正确类型的常量:
template<typename TDestination, typename TSource>
struct boolean_cast_values; // undefined
template<typename TSource>
struct boolean_cast_values<bool, TSource>
{
static const bool true_ = true;
static const bool false_ = false;
};
template<typename TSource>
struct boolean_cast_values<VARIANT_BOOL, TSource>
{
static const VARIANT_BOOL true_ = VARIANT_TRUE;
static const VARIANT_BOOL false_ = VARIANT_FALSE;
};
template<typename TDestination, typename TSource>
TDestination boolean_cast(TSource source)
{
static_assert(std::is_same<TDestination, bool>::value || std::is_same<TDestination, VARIANT_BOOL>::value, "destination must be bool or VARIANT_BOOL");
typedef boolean_cast_values<TDestination, TSource> values;
return source ? values::true_ : values::false_;
}