constexpr 相关问题

constexpr是C ++ 11中引入的修饰符,它通知编译器函数或变量的值是已知的,或者可以在编译时计算。因此,它可以在不可能的地方用作常数。



我的理解是:

恒定表达式不允许包含不确定的行为。

回答 2 投票 0


如何在C ++ 20/23中的SprintF函数验证格式指定符的数量是否与编译时间时提供的参数的数量匹配?

示例,下面的代码最终并未在编译时字符串字符串上的count_percents()调用count_percents(),所以我认为,因为我在sprintf()中对std :: to_string()的使用不是恒定的表达式(使用GCC TRUNK ---- STD = C ++ 2B)。 如果我删除了对std :: to_string()的呼叫,我可以标记两个函数consteval并获取所需的编译时间检查,但是通过在sprintf()中执行其他操作的能力受到严格限制(例如,我不能使用std :: to_string()或创建/操纵,然后返回std :: string)。

回答 1 投票 0




#include<std::jthread> int main(){ std ::vector

#include <thread> #include <vector> #include <iostream> int main() { std::vector<std::jthread> threads; threads.emplace_back([] { std::cout << "New thread via .emplace_back()" << std::endl; }); } 这里,一切都按预期工作正常,但是当我将此代码转换为以下类似的内容时,我会遇到错误:

回答 1 投票 0

在调试模式下constexpr函数的compile-time评估

我一直在玩以下代码片段以了解< 2)

回答 1 投票 0

C++代码在GCC 9.3中编译,但不在GCC10.2

在GCC 9.3中编译以下代码,但在GCC 10.2中不编译: constexpr std ::arrayopt = {1,2}; 模板 constexpr auto f(const t&arr) { std :: ar ...

回答 4 投票 0

在 C++ 中模拟 if __name__ == __main__ 会导致错误“未定义类似函数的宏”

我正在用 bash 编写一个简单的构建文件,这应该让我可以通过模拟 Python 来轻松切换入口点 如果 __name__ == '__main__': 主要的() 我的想法是通过

回答 3 投票 0

如何从 constexpr std::string_view 构造 constexpr static_string

我有一个 static_string 模板 模板 结构静态字符串{ std::array elements_; constexpr static_string() noexcept : elements_{} {} 反对...

回答 1 投票 0

如何使用initializer_list构造函数推导编译时矩阵类的二维?

#包括 #包括 模板 类矩阵 { 受保护: std::array,行>矩阵; 噗...

回答 1 投票 0

在 C 中编译时填充数组

我有一些嵌入式 C 代码,我想要一个正弦值列表。我在 C++ 中使用 constexpr 函数做了类似的事情,该函数在主循环之外返回 std::array : #包括...

回答 1 投票 0

为什么 std::vector::at 不能在使用范围的 for-each 循环中工作?

这是我尝试在 C++26(我知道,没有发布,但这是我给 gcc 的选项)和 GCC 14.2 中的编译时解析器实现的简化示例。 #包括 #包括 这是我尝试在 C++26(我知道,没有发布,但这是我给 gcc 的选项)和 GCC 14.2 中的编译时解析器实现的简化示例。 #include <iostream> #include <ranges> #include <string_view> #include <vector> using namespace std::string_view_literals; template <class To> inline constexpr auto static_caster = []<class From>(From &&from) -> To requires requires { static_cast<To>(std::declval<From>()); } { return static_cast<To>(std::forward<From>(from)); }; struct Test { bool value; constexpr Test(const std::string_view input) { const std::vector<std::string_view> lines = input | std::views::split('\n') | std::views::transform(static_caster<std::string_view>) | std::ranges::to<std::vector>(); // const std::string_view line = lines.at(0); for (const std::string_view line : lines) { const std::vector<std::string_view> line_tokens = line | std::views::split(' ') | std::views::transform(static_caster<std::string_view>) | std::ranges::to<std::vector>(); const std::string_view kind = line_tokens.at(0); this->value = "v" == kind; } } }; int main(int argc, char *argv[]) { constexpr bool value = Test("v 1.0 2.0 3.0\n"sv).value; std::cout << value; return 0; } 错误信息: In file included from /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/vector:68, from <source>:4: /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/bits/stl_vector.h: In function 'int main(int, char**)': <source>:33:52: in 'constexpr' expansion of 'Test(std::literals::string_view_literals::operator""sv(((const char*)"v 1.0 2.0 3.0\012"), 14))' <source>:26:57: in 'constexpr' expansion of 'line_tokens.std::vector<std::basic_string_view<char> >::at(0)' /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/bits/stl_vector.h:1333:16: in 'constexpr' expansion of '((const std::vector<std::basic_string_view<char> >*)this)->std::vector<std::basic_string_view<char> >::_M_range_check(__n)' /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/bits/stl_vector.h:1292:35: error: call to non-'constexpr' function 'void std::__throw_out_of_range_fmt(const char*, ...)' 1292 | __throw_out_of_range_fmt(__N("vector::_M_range_check: __n " | ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1293 | "(which is %zu) >= this->size() " | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1294 | "(which is %zu)"), | ~~~~~~~~~~~~~~~~~~ 1295 | __n, this->size()); | ~~~~~~~~~~~~~~~~~~ In file included from /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/bits/new_allocator.h:35, from /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/x86_64-linux-gnu/bits/c++allocator.h:33, from /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/bits/allocator.h:46, from /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/string:45, from /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/bits/locale_classes.h:42, from /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/bits/ios_base.h:43, from /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/ios:46, from /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/ostream:42, from /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/iostream:43, from <source>:1: /opt/compiler-explorer/gcc-trunk-20241219/include/c++/15.0.0/bits/functexcept.h:82:3: note: 'void std::__throw_out_of_range_fmt(const char*, ...)' declared here 82 | __throw_out_of_range_fmt(const char*, ...) __attribute__((__noreturn__,__cold__)) | ^~~~~~~~~~~~~~~~~~~~~~~~ 这个示例不起作用,但是带有内部 for 循环的示例仅将其内容与索引 0 处的固定行一起使用。 有趣的是,当我从常量中删除 \n 时,它就起作用了。 问题是为什么以及如何解决这个问题? 这个问题与花哨的 c++26 东西无关,它是一个简单(但很容易错过)的错误。如果您以 \n 结尾并在 \n 上拆分,您将得到一个空行。该空行将包含一个空标记。并且空字符串没有“第 0”元素。如果彼此之间有两个空格,也会出现该错误。 要解决此问题,您可以在转换为 std::views::filter([](auto&& x){return !x.empty();}) | 之前在两个循环中添加 vector 以过滤掉空字符串,因为它们不是标记。

回答 1 投票 0

修改 C++17 中 constexpr 函数中的全局变量

在C++17中,是否允许修改constexpr函数中的全局变量? #包括 全局整数 = 0; constexpr int Foo(bool arg) { 如果(参数){ 返回1; }

回答 2 投票 0

编译时变量的唯一哈希值

有没有什么方法可以在编译时为每个变量创建唯一的哈希值。 C++ 允许块作用域,这可能会在一个函数内产生相同的命名变量,所以我不能这样做: #定义宏(...

回答 1 投票 0

在 C++ 中使用 inline 和 constexpr

我正在为 FRC 机器人开发一个机器人程序,并且很好奇 inline 或 constexpr 或两者是否都适合声明常量。 内联 constexpr 双 PI = wpi::math::pi 内联

回答 1 投票 0

如何在 C++ 的编译时上下文中将值重新解释为字节

我正在尝试实现一个可以在编译时使用的哈希函数。为了实现这一点,我需要将一个值(例如,int 或 float)重新解释为其原始字节。但是,当使用编译时

回答 1 投票 0

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.