使用 c++20 检查 type 是否具有某些值类型和关键字 value_type 本身

A2LBK

我有以下代码

#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;
}

我基本上想确保模板参数具有某些类型,整数或浮点数。有两个问题:

  1. 代码无法编译。抱怨它需要 '(' 用于函数样式转换或类型构造。不知道为什么?
  2. 我假设 T,U,Tout 都有关键字value_type如果有人将原始指针传递给整数类型或浮点数,我还希望该类能够计算出来。有没有一种简单的方法来合并它?
  3. 如果没有,当有人试图传递一个没有 value_type
伍德福德
  1. 您发布的代码中有几个拼写错误。清理完毕,以下内容可以满足您的需求:
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;
};
  1. 这是一个很大的问题。也许其他人可以填补这个空白。
  2. 编译器生成的错误消息(此处为 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过在 C++ 中使用模板获取值和指针的 Value_Type?

如何使用C ++ 11 integer_constant :: value_type()

在 C++20 中什么时候应该使用 `iterator_traits<I>::value_type`,什么时候应该使用 `iter_value_t`?

使用 value_type 在 pertect forward 中获取类型

如何使用C ++ 20概念检查模板本身中的一些约束

C ++何时以及如何使用std :: iterator value_type,引用,指针?

C ++在'struct std :: iterator_traits <int>'中没有名为'value_type'的类型

type,value_type和element_type之间的区别是什么?何时使用它们?

使用“ typename Container :: value_type”作为返回值时,无法推断模板参数

使用c ++ 20 Concept检查函数是否返回const值

具有复杂值类型的迭代器:与value_type和reference混淆

C ++ 11从变量获取std :: map value_type

什么时候使用迭代器的“ value_type”?

具有自定义键的C ++ 11兼容集(key_type不是value_type,但函子获得了value的键)

引用绑定到类型为'value_type'的空指针

STL 迭代器继承:“value_type”未命名类型

C#-如何检查Type是否具体?

使用'type'关键字和依赖于路径的类型覆盖类型

模板内的 C++ value_type::second_type 编译器错误

从具有相同容器类型但value_type不同的容器生成新类型

為什麼 std::set 中有 key_type 和 value_type

如何解决“无法使用类型为value_type(又称为Derived *)的左值初始化Base *类型的返回对象?

我们如何打印出C ++ STL容器的value_type?

为什么 C++ 分配器要求不要求construct() 构造value_type 的对象?

使用value_type的unique_ptr构造unordered_map

使用 C++20 在编译时检查容器中是否存在重复元素

如何只允许具有ceratin value_type的迭代器?

Mypy不使用Type [NamedTuple]进行类型检查功能

如何使用工具检查xsd模式本身是否有效?