使用 c++20 检查 type 是否具有某些值类型和关键字 value_type 本身 [英] check if type has certain value types and the keyword value_type itself using c++20
本文介绍了使用 c++20 检查 type 是否具有某些值类型和关键字 value_type 本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码
#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;
}
我基本上想确保模板参数具有某些类型,整型或浮点型.有两个问题:
I essentially want to ensure that the template argument have certain types, integral or floating point. There are two issues:
- 代码无法编译.抱怨它需要 '(' 进行函数样式转换或类型构造.不知道为什么?
- 我假设 T、U、Tout 都有关键字
value_type
.如果有人将原始指针传递给整数类型或浮点数,我还希望该类能够计算出来.有没有一种简单的方法来合并它? - 如果不是,当有人试图传递一个没有
value_type
的模板参数时,我们如何确保错误信息非常清楚
- The code does not compile. Complains that it needs '(' for function style cast or type construction. Not sure why?
- I am assuming that T,U,Tout all have the keyword
value_type
. I also want the class to work out if somebody is passing a raw pointer to an integral type or floating point. Is there an easy way to incorporate that? - If not, how can we ensure that the error message is very clear when somebody tries to pass a template argument which does not have the
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
这篇关于使用 c++20 检查 type 是否具有某些值类型和关键字 value_type 本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文