- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我发现 noexcept
的奇怪行为C++14 中的运算符。
以下代码可以通过 gcc 和 clang(使用 --std=c++14 选项)很好地编译。
// test.cpp
#include <iostream>
#include <type_traits>
#if 1
#define TESTREF(X) X&&
#else
#define TESTREF(X) X const&
#endif
template <class F, class... Args>
struct is_noexcept_callable
: public std::conditional_t<noexcept(std::declval<F>()(std::declval<Args>()...)), std::true_type, std::false_type> {};
template <
class F,
std::enable_if_t<is_noexcept_callable<F,int>::value,int> = 0
>
int evalInt(int x, TESTREF(F) f) noexcept
{
return static_cast<int>(f(x));
}
template <
class F,
std::enable_if_t<!is_noexcept_callable<F,int>::value,int> = 0
>
int evalInt(int x, TESTREF(F) f)
{
return static_cast<int>(f(x));
}
int id(int x) noexcept { return x; }
int thrower(int x) { throw(0); }
int main(int argc, char* argv[])
{
std::cout << std::boolalpha
<< noexcept(evalInt(1,id))
<< std::endl;
std::cout << std::boolalpha
<< is_noexcept_callable<decltype(thrower), int>::value
<< std::endl;
}
$ g++ --std=c++14 test.cpp
$ ./a.out
true
false
$ clang++ --std=c++14 test.cpp
$ ./a.out
false
false
#if 0
然后 gcc 将代码编译成另一个不同的程序:
$ ./a.out
true
true
noexcept
thrower
规范宏不接触的功能。
最佳答案
我只能在版本 8 之前的 GCC 中重现这个错误。行为的差异是由于 noexcept
说明符是 GCC 7 的 C++14 版本(但不是 Clang 的)中函数类型的一部分,尽管这是 C++17 的特性。如果我们添加 is_noexcept_callable
的部分特化,就可以看到这一点。 :
template <class... Args>
struct is_noexcept_callable<int(&)(int), Args...>
: public std::false_type {};
template <class... Args>
struct is_noexcept_callable<int(int), Args...>
: public std::false_type {};
false
s : GCC 保留了
noexcept
函数类型上的属性,但在模板参数推导期间显式忽略它们,以便选择上述特化,尽管错误消息显示
noexcept
如果我们删除定义:
prog.cc:30:5: note: template argument deduction/substitution failed:
prog.cc:28:22: error: incomplete type 'is_noexcept_callable<int (&)(int) noexcept, int>' used in nested name specifier
TESTREF
的定义影响
is_noexcept_callable
?
is_noexcept_callable
已经使用相关类型实例化
int(int) [noexcept]
在您使用它之前
main
,但它没有附加,所以
is_noexcept_callable<int(int), int>::value
的结果固定为true。
decltype(id)
是
int(int) [noexcept]
,其中
[noexcept]
是我的符号来表达附加到函数类型的 GCC 的 transient 异常规范。因此
evalInt(1,id)
导致实例化
is_noexcept_callable<F,int>
哪里 F = int(&)(int) [noexcept]
当TESTREF = X&&
和 F = int(int) [noexcept]
当TESTREF = X const&
is_noexcept_callable<int(int),int>::value == true
之后持有
noexcept(evalInt(1,id))
已处理,因为
id
是 noexcept 并且这会沿着实例化链向下传播。
int main(int argc, char* argv[])
{
std::cout << std::boolalpha
<< noexcept(evalInt(1,thrower))
<< std::endl;
std::cout << std::boolalpha
<< is_noexcept_callable<decltype(thrower), int>::value
<< std::endl;
}
关于c++ - C++14 中 noexcept 说明符的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60201135/
我得到以下声明: // file MadaPacket.h class MadaPacket { // .... public: inline static bool word_is_header
目录 1.语法 2.关键词decltype 1.语法 decltype ( 实体 ) (1) (C++11 起) decltype
由于某些原因,我一直认为演绎指南必须相同noexcept -它们所引用的构造函数的性质。例如: template struct clazz { clazz(const T &) noexcep
我不确定成员 var isMouseOverYard 的正确访问说明符。在代码片段中,我没有从 House 继承的计划。选项 1 与基类更一致(如果我要从任一类继承,我可以检查鼠标是否在对象/院子上)
我可以声明 foo(const T& var) 这样我就知道 var 不会被改变。 指针的等效格式为 foo(const T* var)? 过去我尝试过那些,与 iterator/const_iter
我已经为这个问题搜索了几个小时,但仍然无法解决。 #include using namespace std; enum color { brown, green, orange, red, yell
我有用户定义的数据类型 typedef Unsigned int8 COMMAND_TYPE[6]; 现在我有类似的功能 ConnectCommand(COMMAND_TYPE const comm
说明符 %[^s] 有什么用? s 是一个变量。 在什么情况下我可以使用这个说明符? 最佳答案 scanf 的 %[ 格式说明符将匹配一系列字符,这些字符与 [ 和 ]< 之间列出的字符相匹配。如果第
#include int main() { char a[8]; printf("%d\n",a) ; return 0; } 对于上面的代码,输出是这
很抱歉这个“另一个”sscanf 问题,但我无法通过实验找到任何解决方案。 这是一个字符串,我想解析并提取 2 个由“:”分隔的子字符串: char *str = "tag:R123:P1234";
所以我在维基百科的一篇文章(粗略翻译)中遇到了以下定义: Modifier (programming) - element of source code being a phrase of given
[basic.link]/6 (我的重点): The name of a function declared in block scope and the name of a variable dec
我正在尝试定义我自己的数据类型(称为 sfloat),它类似于 float ,但使用不同数量的尾数位和指数位以更好地适应我的数据范围和精度。目标是定义一种新的数据类型,可以替代现有应用程序中的 flo
请看下面的代码: #include struct A { A(int, int) {} }; struct tag {}; template struct is_noexcept { st
如果这是一个无知的问题,请原谅我,但我仍在思考何时以及如何使用 constexpr 说明符。 (使用 msvc 14 编译)。我正在研究一个简单的基类,它允许您将任意对象包装到“constexpr 对
考虑以下函数: // Declaration in the .h file class MyClass { template void function(T&& x) const; }; /
以下面的示例代码为例: void test(const Item& item = Item()) { ... } 假设一旦 item 被传递给函数,this 就不能抛出。 问题是:函数应该标记为
我听说 noexcept 关键字更像是“它永远不应该抛出异常”而不是“它不会”。 如果我不确定是否抛出异常,我认为使用 noexcept 关键字不是很好,但是 noexcept 关键字有时
最近,我在阅读API of boost::optional 时发现: T const& operator *() const& ; T& operator *() & ; T&&
如果覆盖 ToString在一个类型中 type TestMe ()= override __.ToString() = null 然后我通过 "%A" 输出它说明符 printfn "*%A
我是一名优秀的程序员,十分优秀!