模板不同的返回类型
template different return types
本文关键字:返回类型 更新时间:2023-10-16
在下面的代码中,我必须将boolean_cast转换为TDestination以避免编译器警告(从"VARIANT_BOOL"截断为"BOOL")。这是编译器问题还是C++问题?
template<typename TDestination, typename TSource>
TDestination boolean_cast(TSource source)
{
TDestination destination;
static_assert(std::is_same<TDestination, bool>::value || std::is_same<TDestination, VARIANT_BOOL>::value, "destination must be bool or VARIANT_BOOL");
//convert to bool
if (std::is_same<TDestination, bool>::value)
{
if (source)
destination = true;
else
destination = false;
}
//convert to VARIANT_BOOL
else
{
if (source)
destination = (TDestination)VARIANT_TRUE;
else
destination = (TDestination)VARIANT_FALSE;
}
return destination;
}
正如@n.m在一条注释中所说,编译器不会停止编译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_;
}
相关文章:
- 如何获取std::result_of函数的返回类型
- 奇怪的结构&GCC&clang(void*返回类型)
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 函数作为模板参数,是否对返回类型强制约束
- C++中函数的向量返回类型引发错误
- 检查函数返回类型是否与STL容器类型值相同
- 为什么返回类型中需要typename?C++
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 为什么 c++(g++) 不允许模板返回类型和函数名称之间有空格?
- 为什么返回类型的'const'限定符对标有 __forceinline/内联的函数没有影响?
- 推导 std::vector::back() 的返回类型
- 在 c++ 中将函数返回类型指定为模板参数
- 使用 SWIG 更改生成的 CS 函数中的返回类型
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 基于返回类型的转换和过载扣除
- 当返回类型声明为 ListNode 时,我们是否可以返回 false<T>*