使用 c++20 检查类型是否具有某些值类型和关键字 value_type 本身
check if type has certain value types and the keyword value_type itself using c++20
我有以下代码
#include <type_traits>
template<typename T, typename U, typename Tout>
requires std::is_integral< typename T::value_type >
&& std::is_floating_point< typename U::value_type >
&& std::is_floating_point< typename Tout::value_type >
class test
{
test() =default;
};
int main(int argc, char const *argv[])
{
/* code */
return 0;
}
我基本上想确保模板参数具有特定类型,整数或浮点数。
有两个问题:
- 代码无法编译。抱怨它需要'('来进行函数样式转换或类型构造。不确定为什么?
- 我假设 T、U、Tout 都有关键字
value_type
。如果有人将原始指针传递给整数类型或浮点数,我还希望 class 能够计算出来。有没有简单的方法来合并它?
- 如果不是,当有人试图传递没有
value_type
的模板参数时,我们如何确保错误消息非常清楚
- 您发布的代码中有几个拼写错误。清理完毕,以下就是您要查找的内容:
template<typename T, typename U, typename Tout>
requires std::is_integral_v<typename T::value_type>
&& std::is_floating_point_v<typename U::value_type>
&& std::is_floating_point_v<typename Tout::value_type>
class test {
public:
test()=default;
};
- 这是一个很大的问题。也许其他人可以填补空白。
- 编译器(此处为 g++ 10.2)生成的错误消息似乎很清楚:
int main() {
test<int, int, int> t;
}
main.cpp: In function 'int main()':
main.cpp:16:21: error: template constraint failure for 'template<class T, class U, class Tout> requires (is_integral_v<typename T::value_type>) && (is_floating_point_v<typename U::value_type>) && (is_floating_point_v<typename Tout::value_type>) class test'
16 | test<int, int, int> t;
| ^
main.cpp:16:21: note: constraints not satisfied
我有以下代码
#include <type_traits>
template<typename T, typename U, typename Tout>
requires std::is_integral< typename T::value_type >
&& std::is_floating_point< typename U::value_type >
&& std::is_floating_point< typename Tout::value_type >
class test
{
test() =default;
};
int main(int argc, char const *argv[])
{
/* code */
return 0;
}
我基本上想确保模板参数具有特定类型,整数或浮点数。 有两个问题:
- 代码无法编译。抱怨它需要'('来进行函数样式转换或类型构造。不确定为什么?
- 我假设 T、U、Tout 都有关键字
value_type
。如果有人将原始指针传递给整数类型或浮点数,我还希望 class 能够计算出来。有没有简单的方法来合并它? - 如果不是,当有人试图传递没有
value_type
的模板参数时,我们如何确保错误消息非常清楚
- 您发布的代码中有几个拼写错误。清理完毕,以下就是您要查找的内容:
template<typename T, typename U, typename Tout>
requires std::is_integral_v<typename T::value_type>
&& std::is_floating_point_v<typename U::value_type>
&& std::is_floating_point_v<typename Tout::value_type>
class test {
public:
test()=default;
};
- 这是一个很大的问题。也许其他人可以填补空白。
- 编译器(此处为 g++ 10.2)生成的错误消息似乎很清楚:
int main() {
test<int, int, int> t;
}
main.cpp: In function 'int main()':
main.cpp:16:21: error: template constraint failure for 'template<class T, class U, class Tout> requires (is_integral_v<typename T::value_type>) && (is_floating_point_v<typename U::value_type>) && (is_floating_point_v<typename Tout::value_type>) class test'
16 | test<int, int, int> t;
| ^
main.cpp:16:21: note: constraints not satisfied