- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下代码。我可以用 GCC 10.2.0 和 Clang 11.0.0 编译它(正如预期的那样):
#include <iostream>
template<int>
struct T {
static constexpr auto fun() noexcept { return 0; }
using type = std::remove_cvref_t<decltype(fun())>;
};
int main() {
decltype(T<1>::fun()) a = 1;
std::cout << a;
}
如果我更换
constexpr
与
consteval
,然后 Clang 提示
std::remove_cvref_t<decltype(fun())>
:
error: cannot take address of consteval function 'fun' outside of an immediate invocation
最佳答案
正如在对问题的评论中已经说过的那样,这是 CLang 错误。
仅当函数是静态方法时才会出现此错误,如果它是全局函数,则代码有效(参见工作 online example here)。
因此,解决此问题的一种方法是使用全局函数来转发静态方法的结果。我在下面做了一个关于这种全局转发的更高级的例子。
Try it online!
#include <iostream>
#include <type_traits>
template <typename T, typename ... Args>
static consteval auto forward_fun(Args ... args) {
return T::fun(args...);
}
template <int I>
struct T {
static consteval auto fun(int i, bool f) noexcept {
return i + 1;
}
using type = std::remove_cvref_t<decltype(forward_fun<T>(123, true))>;
};
int main() {
T<1>::type a = 1;
std::cout << a;
return 0;
}
关于c++ - decltype(fun()) 用于 consteval 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64762278/
在 C++17 中,这段代码是非法的: constexpr int foo(int i) { return std::integral_constant::value; } 那是因为即使 fo
在下面的代码中,如果 here()不再是 consteval (完整的 RT 或 constexpr ),然后是 line()是 f() 的调用行里面 main() .但与 consteval这是f(
在 C++17 中,这段代码是非法的: constexpr int foo(int i) { return std::integral_constant::value; } 那是因为即使 fo
显然,consteval 将成为 C++20 中的关键字。 cppreference page因为它目前是空白的。它将是什么以及它与 constexpr 有何关系? 最佳答案 它声明了立即函数,即必须
我正在使用整洁的 fmt 库,在其版本 8 中,如果编译器支持相关功能,则会对其格式字符串进行编译时检查。 在某个时候,我想编写以下代码: throw my_exception("error: {}"
C++23即将引入if consteval .这将在哪里使用,它与 constexpr if 有何不同? ? 最佳答案 if consteval检测是否有 constexpr函数在常量表达式上下文中调
我有以下功能: template consteval size_t indexOf(SomeEnum someEnum, const std::ar
有时函数的结果不能用单个返回值来表示。例如:与两条线相交的函数。人们可能希望函数返回实际的交点以及它们之间的关系(即平行、相同、相交或倾斜)。 让我们假设这个例子,其中交点由某种类表示,线的位置关系由
这可能是一个愚蠢的问题,但我很困惑。我有一种感觉,立即(consteval)函数必须在编译时执行,而我们根本无法在二进制文件中看到它的主体。 This article明显支持我的感觉: This ha
有时函数的结果不能用单个返回值来表示。例如:与两条线相交的函数。人们可能希望函数返回实际的交点以及它们之间的关系(即平行、相同、相交或倾斜)。 让我们假设这个例子,其中交点由某种类表示,线的位置关系由
我在 C++20 模式下使用 GCC10 尝试了以下第一个变体: consteval std::experimental::source_location there() { return st
我正在编写一个类来包装与设备一起使用的基于 C 的库的一部分,其中每个设备都配置有回调函数指针来处理数据。将为每个设备创建一个 MyClass 实例。见下文: struct DeviceConfig
假设我们有一个 consteval 函数或一个带有 consteval 构造函数的普通结构,它只接受一些值: struct A { consteval A(int a) {
我很好奇就编译时评估而言,我可以将 gcc 推多远,所以我让它计算了 Ackermann函数,特别是输入值为 4 和 1(高于此值的任何值都是不切实际的): consteval unsigned in
C++20 将有新的 consteval关键字和constexpr destructor如果一切顺利。遗憾的是,据我所知,没有编译器实现 consteval此时。下面的代码有效吗? struct A
目前您不能使用 static_assert 来验证 constexpr 函数的参数,即使对它的所有调用确实都是 constexpr。这是有道理的,因为编译器仍然必须创建此函数的非 constexpr
我知道需求的差异,我最感兴趣的是它带来的代码质量带来的好处。 我能想到的几件事: reader 可以读取函数签名并知道函数在编译时被评估 编译器可能会发出更少的代码,因为 consteval fns
考虑以下代码。我可以用 GCC 10.2.0 和 Clang 11.0.0 编译它(正如预期的那样): #include template struct T { static constexpr
我正在尝试创建一个遵循构建器模式并在编译时完全运行的类(使用 C++20 中的新 consteval 关键字),但无论我尝试什么都行不通。例如,这将不起作用: #include class MyCl
我正在尝试创建一个遵循构建器模式并在编译时完全运行的类(使用 C++20 中的新 consteval 关键字),但无论我尝试什么都行不通。例如,这将不起作用: #include class MyCl
我是一名优秀的程序员,十分优秀!