- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我意识到这是一个非常基本的问题,但我只是想确认我是否正确使用了 std::enable_if
,因为我有点不确定“正确”的错误消息应该是什么尝试调用已禁用的函数时。
考虑以下程序 ( link ),预计不会编译:
#include <type_traits>
template <int Z=0> std::enable_if_t<Z> function () { }
int main () {
function();
}
GCC 9在C++17模式下输出的错误信息是:
g++ --std=c++17 -W -Wall -pedantic smelly_template.cpp -o build-c++17/smelly_template
smelly_template.cpp: In function ‘int main()’:
smelly_template.cpp:6:12: error: no matching function for call to ‘function()’
6 | function();
| ^
smelly_template.cpp:3:40: note: candidate: ‘template<int Z> std::enable_if_t<(Z != 0)> function()’
3 | template <int Z=0> std::enable_if_t<Z> function () { }
| ^~~~~~~~
smelly_template.cpp:3:40: note: template argument deduction/substitution failed:
In file included from smelly_template.cpp:1:
/usr/include/c++/9/type_traits: In substitution of ‘template<bool _Cond, class _Tp> using enable_if_t = typename std::enable_if::type [with bool _Cond = (0 != 0); _Tp = void]’:
smelly_template.cpp:3:40: required by substitution of ‘template<int Z> std::enable_if_t<(Z != 0)> function() [with int Z = 0]’
smelly_template.cpp:6:12: required from here
/usr/include/c++/9/type_traits:2384:11: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
2384 | using enable_if_t = typename enable_if<_Cond, _Tp>::type;
| ^~~~~~~~~~~
我的问题很简单:这些是我在正确使用 enable_if
时应该看到的错误消息吗(例如,它们只是关于编译器试图做什么的注释),还是没有我搞砸了什么?
我不确定的原因是:
enable_if
的帖子、教程和文档中,每当有人遇到“enable_if 中没有名为‘type’的类型”错误时,似乎总是因为他们我做错了什么(粗略搜索:example、example、example,不胜枚举)。所以我想知道是否我做错了什么,因为我也看到了“没有名为‘type’的类型”错误。
虽然我确实看到了最终的预期行为——编译失败——但我现在更关心的是完全正确的用法,而不是简单地以某种形式满足程序要求(编译失败)。
最佳答案
std::enable_if_t
是一个模板,如果条件为假,它会导致替换失败。由于 SFINAE,当它发生在重载解析期间时,这不会导致程序格式错误。您将一个假值作为默认值传递给它,因此对于对 function()
的调用, 如果没有任何额外指定的模板参数,重载解析将失败。
如果你改变template <int Z = 0>
属于 int Z = 1
那么我希望代码能够编译。
问题的第二部分的更多内容:这些其他错误是预期的吗?
smelly_template.cpp:3:40: note: candidate: ‘template<int Z> std::enable_if_t<(Z != 0)> function()’
3 | template <int Z=0> std::enable_if_t<Z> function () { }
| ^~~~~~~~
smelly_template.cpp:3:40: note: template argument deduction/substitution failed:
In file included from smelly_template.cpp:1:
是的,每当重载解析失败时,编译器都会尝试通过显示它尝试过的内容来帮助您。现代版本的 gcc 和 clang 将向您显示每个可用的重载以及不能使用的原因。在这种情况下,它解释了为什么重载解析在它尝试过的一个重载中失败了。重载解析失败时出现的这类错误在大型程序中极其很有帮助。
关于c++ - 这些是 std::enable_if 的预期错误,还是我使用不当?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65510782/
我正在使用这样的模板别名: template ::value>::type> using vec2 = std::pair; template ::value>::type> using vec3 =
基本问题陈述 我正在学习 SFINAE。我尝试了一个非常简单的 enable_if : // 1: A foo() that accepts arguments that are derived fr
在language reference of std::enable_if at cppreference包括以下注释 Notes A common mistake is to declare two
我对 SFINAE 有基本的了解,我想我理解了很多关于如何 std::enable_if 的例子。利用它来选择函数模板特化,但我很难理解它如何用于类模板。 以下例子来自cppreference.com
这有什么问题? 我认为这应该在使用 enable if 时起作用??? 帮助?? 不应该排除第二个构造函数吗? #include #include #include template class
我试图在模板类中声明函数,以便函数声明依赖于模板类型参数。 template struct Block { static bool parse(int32_t index,
我正在尝试一种基于类模板参数来专门化成员函数的方法,而不必在类上使用 SFINAE(并导致代码重复或创建另一个类)。 由于两个模板参数不能是可选的,并且参数 enable_if 在指南中是不受欢迎的,
我遇到一个问题,未知代码正在使用试图在编译时和运行时取消引用类型的元模板。这意味着,它们遍历指针层次结构直到找到匹配器。 现在通常这很好用。但是当用户传递类似的东西时: typedef struct
我试图了解 std::enable_if 是如何工作的 inn 模板参数。 #include #include #include using namespace std; class Inter
有谁知道为什么下面的代码可以编译 static const size_t CONSTANT = /* ... */; template = 0 > res_type foo() { // ...
我有一个类定义为: template class AdjacencyList; 其中 V 和 E 分别是顶点和边值的类型。 我目前正在尝试在 AdjacencyList 中定义以下成员函数: std
我有以下代码无法在 VC2010 上编译: #include using namespace std; template typename enable_if::type foo() { retu
为什么编译器对 std::tuple 的访问者具有以下定义顺序很重要 namespace TupleVisit{ //This function SHOULD BE DEFINED S
我想使用 type_traits 是否通过 shared_ptr 重载。 struct A { A(int i) : x(i) {} int x; }; int main() {
作为一个实验,我试图使一个没有参数的 void 成员函数根据类模板参数改变行为: #include #include template class MyClass { public: void
我正在使用 enable_if 语句来删除考虑中的可能方法。 #include "gmpxx.h" #include template struct is_ring_field { }; temp
是否有任何“更干净”的方式(我的意思是减少重复代码)来编写以下内容? template class Test { struct Foo1 { int a; }; struct F
在C ++中,请考虑以下示例: template struct q; template struct q { q() { cout struct q {
我对 std::enable_if 很陌生,想知道如何使用它。我有一个模板类: template class foo { } 现在我只想在 a + b 等于 10 时实例化它。我可以使用 std::e
我最近遇到了一个有趣的 enable_if 用法版本,它用于有条件地启用具有更好可读性的函数,因为该函数的返回类型不是 enable_if 的一部分(请参阅 cleaner_usage): #incl
我是一名优秀的程序员,十分优秀!