- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用新概念语法为自己做一个简单的例子。我决定测试一个类型是否定义了 operator(),并创建了一个结构来使用 SFINAE 范例对此进行测试,但我遇到了类型问题。这是我的代码:
#include <utility>
#include <functional>
namespace Templates::Concepts {
template<class type__>
struct call_check {
template<class type_ = type__>
static auto check(std::nullptr_t) -> decltype(std::declval<type_>().operator()(), std::false_type(), std::true_type());
template<class type_ = type__>
static auto check(...) -> decltype(std::false_type());
template<class type_ = type__>
using type = decltype(check<type_>(nullptr));
};
template<typename type_>
concept bool Callable = []() -> bool { typename call_check<type_>::type *t; return *t;};
}
我一开始没有'typename'指针,只是有
return call_check<type_>::type;
,
但我收到了与名称相关的类型错误。添加我现在收到的类型名后
concepts.h:20:78: error: ‘typename Templates::Concepts::call_check<yes>::type’ names ‘template<class type_> using type = decltype (check<type_>(nullptr))’, which is not a type
,
我卡住了。坦率地说,我不完全确定执行此 SFINAE 检查的最正确方法是什么,因此我不确定从哪里开始。任何有关范式和/或概念的帮助也将不胜感激。
我确实看到了一个类似
的例子 std::declval<type_>()(std::declval<other>(), std::declval<op>()), ...
替换第一次检查的 decltype 调用中的第一项(对于二元运算符),但我很难理解它是如何转换为函数调用的。 (倒数第三个答案,供引用:How to check whether operator== exists?)。
最佳答案
让我们来处理您的原始代码:
template<class type__>
任何地方的双下划线都保留给实现。
struct call_check {
template<class type_ = type__>
static auto check(std::nullptr_t) -> decltype(std::declval<type_>().operator()(), std::false_type(), std::true_type());
您通常不想检查某物是否具有operator()
;你想检查它是否可以不带参数调用,并且 , std::false_type()
部分没有意义,所以尾随返回类型应该是
-> decltype(std::declval<type_>()(), std::true_type())
template<class type_ = type__>
static auto check(...) -> decltype(std::false_type());
这是不必要的冗长。 decltype(std::false_type())
只是 std::false_type
。也不需要默认模板参数,因为您没有使用它,因此它变成了
template<class>
static std::false_type check(...);
template<class type_ = type__>
using type = decltype(check<type_>(nullptr));
这里没有理由将其设为别名模板。它应该只是一个别名:
using type = decltype(check<type__>(nullptr)); // modulo reserved identifier.
};
template<typename type_>
concept bool Callable = []() -> bool { typename call_check<type_>::type *t; return *t;};
这在很多方面都是错误的。 TS 中的变量概念必须用常量表达式初始化,在 C++17 之前的常量表达式中不能使用 lambda。此外,您没有调用 lambda,因此您将其隐式转换为函数指针,然后转换为 bool
,这始终会产生 true
.最后,实际调用 lambda 将是未定义的行为,因为它解引用了一个未初始化的指针。
最简单的拼写方式是
template<typename type_>
concept bool Callable = call_check<type_>::type::value;
关于c++ - 类型名称的概念/SFINAE 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51003709/
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
我正在开发一个 Android 应用程序。在此应用程序中, Logo 栏显示在所有页面( Activity )上,或者我们可以说它在所有页面上都有标题。这个 Logo 栏有几个图标,如主页、登录、通知
我正在使用 hadoop 使用开源接口(interface) HVPI 处理视频。然而,inputsplit 的实现,更准确地说是在 isSplitableobContext (context, Pa
1. 是什么? MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System
有没有办法使用 c++20s 的概念来检查一个值是否满足某些要求? 假设我正在编写某种使用分页的容器,并且我想让页面大小成为模板参数。 template class container; 我可以使用带
如何在 ArrayList 中循环遍历 ArrayList? 例如,如果我有一个名为 Plants of Plant 对象的 ArrayList。每个 Plant 对象内部都有一个随机数量的花名。我如
如何在UML类图中绘制C++概念? 具体来说,我有以下代码: template concept Printable = requires(T a, std::ostream &where) {
我有兴趣制作一个网站,在访问者访问时闪现整个网络历史记录。我计划使用 JavaScript 来获取每个观看者计算机上的历史记录,并根据他们拥有的内容以不同的速度对其进行动画处理。我的想法是使用 his
有一个模板定义,例如: template void foo( void ) { /* ... */ } 如何定义一个概念,以便N必须为非零正值(N> = 1)? 就像是: template con
封装是信息隐藏还是导致信息隐藏? 正如我们所说,封装将数据和函数绑定(bind)在单个实体中,因此它为我们提供了对数据流的控制,并且我们只能通过一些定义良好的函数来访问实体的数据。因此,当我们说封装导
下面有一个简单的代码片段,它使用以下方式进行编译: g++-9 -std=c++2a -fconcepts 这是试图定义一个需要存在函数的概念。我希望输出是"is",但事实并非如此……知道为什么吗?谢
我有一个普通二元运算符的概念 template concept is_binary_operation = requires (const T& t1, const T& t2) // e.g
我正在c++ 20中实现具有启发式功能的搜索算法。 我试图用类似这样的概念来约束我的算法可以使用的功能: template concept Heuristic = requires(SelfType
我需要了解 SAS 如何读取/执行数据步骤。当我查找有关 SAS 如何读取数据步骤的信息时,我似乎只找到有关它如何读取以进行合并的信息,我不了解与常规数据步骤相关的信息。比方说,我有这行代码: dat
最近我看到一个关于“框架”的问题,如果“框架”有不同的类型或概念。那么,存在不同“类型”的“框架”吗? 例如:NodeJS 是一种“类型”(概念),而 Hibernate ORM 是另一种“类型”(概
如何使用任何技术禁用或清除客户端浏览器 Cookie 我认为使用 javascript 可以用于任何技术 最佳答案 var cookies = document.cookie.split(";");
我正在使用 target = "_blank" 单击链接时生成新选项卡。但是,浏览器会将焦点移至该选项卡。 有没有办法让焦点保持在当前标签页上? 回答摘要 基本上,只需发送一个模拟控件点击的当前事件。
我正在尝试在我的 android/firebase(cloud firestore) 应用程序上添加一项需要其他用户批准/拒绝的功能。例如,当 Air&BnB 上的用户想要预订一个地方时,所有者必须批
这个问题在这里已经有了答案: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expec
public class MyClass { public static void main(String[] args) { System.out.println("Hell
我是一名优秀的程序员,十分优秀!