constexpr

constexpr-ness of std::initializer_list::size() vs std::array::size()

孤者浪人 提交于 2021-01-27 18:38:10
问题 The size() member functions of std::initializer_list and std::array have identical signatures: constexpr size_type size() const noexcept; Both are constexpr . However, std::array::size() can be used in constexpr context, but std::initializer_list::size() can't: std::initializer_list<int> il{1, 2, 3, 4}; constexpr std::size_t il_size = il.size(); // (1) - fails with GCC and Clang (*) std::array<int, 4> arr{1, 2, 3, 4}; constexpr std::size_t arr_size = arr.size(); // (2) - OK (*) The error is:

How to create a constexpr array with a sequence of string_views at compile time?

醉酒当歌 提交于 2021-01-27 06:12:06
问题 I would like to create a constexpr std::array<std::string_view, ConstexprNumber> . It should for example contain constexpr std::strings_view's like this: "text0", "text1", "text2", ..... "textn" I came up with the following initial solution: #include <iostream> #include <array> #include <utility> #include <string> #include <string_view> // Number of strings that we want to generate constexpr size_t NumberOfTextsToGenerate = 10u; // constexpr function to build a string constexpr std::string

Why constexpr must be static?

独自空忆成欢 提交于 2021-01-27 04:07:04
问题 An attempt to create a member of a struct with constexpr attribute without being static result in a compiler error(see below). Why is that? for a single constant value will I have this value in memory until program is terminatted instead of just scope of struct? should I back to use a macro? struct foo { constexpr int n = 10; // ... }; error: non-static data member cannot be constexpr; did you intend to make it static? 回答1: I don't know the official rational. But surely it could lead to

Why constexpr is not the default for all function? [duplicate]

一个人想着一个人 提交于 2021-01-26 20:35:48
问题 This question already has answers here : Why do we need to mark functions as constexpr? (4 answers) Closed 4 years ago . After relaxing the rules for the constexpr it seems as these functions can be used everywhere. They can be called on both constant (constexpr) and local (mutable) variables as well. So for me it seem as just a hint for the compiler (like inline). I just keep writing it everywhere and remove it if compiler complains. So compiler seems to know everything if a function can be

Why constexpr is not the default for all function? [duplicate]

混江龙づ霸主 提交于 2021-01-26 20:34:35
问题 This question already has answers here : Why do we need to mark functions as constexpr? (4 answers) Closed 4 years ago . After relaxing the rules for the constexpr it seems as these functions can be used everywhere. They can be called on both constant (constexpr) and local (mutable) variables as well. So for me it seem as just a hint for the compiler (like inline). I just keep writing it everywhere and remove it if compiler complains. So compiler seems to know everything if a function can be

Is `const constexpr` on variables redundant?

自闭症网瘾萝莉.ら 提交于 2021-01-21 03:52:25
问题 cppreference states that: A constexpr specifier used in an object declaration or non-static member function (until C++14) implies const. Does "object declaration" mean "any variable declaration"? I.e. is constexpr const int someConstant = 3; equivalent to constexpr int someConstant = 3; in C++11, C++14 and C++17? 回答1: In declarations with primitives, such as the one in your example, const is indeed redundant. However, there may be odd situations where const would be required, for example

Is `const constexpr` on variables redundant?

匆匆过客 提交于 2021-01-21 03:52:05
问题 cppreference states that: A constexpr specifier used in an object declaration or non-static member function (until C++14) implies const. Does "object declaration" mean "any variable declaration"? I.e. is constexpr const int someConstant = 3; equivalent to constexpr int someConstant = 3; in C++11, C++14 and C++17? 回答1: In declarations with primitives, such as the one in your example, const is indeed redundant. However, there may be odd situations where const would be required, for example

Why a std::array is not constant expression when it is the input of a templated function/generic lambda?

吃可爱长大的小学妹 提交于 2021-01-05 07:08:39
问题 (Realted to this other question of mine; if you give a look at that too, I would really appreciate it.) If std::array<T,N>::size is constexpr, then why does the following code not even compile? #include <array> #include <iostream> constexpr auto print_size = [](auto const& array){ constexpr auto size = array.size(); std::cout << size << '\n'; }; int main() { print_size(std::array<int,3>{{1,2,3}}); } The error is the following: $ g++ -std=c++17 deleteme.cpp && ./a.out deleteme.cpp: In

How will C++20 constexpr containers work?

混江龙づ霸主 提交于 2020-12-25 04:10:28
问题 As constexpr std::string and constexpr std::vector have been accepted into C++20, how will these be used? The linked papers are very short on details. Do we need to specify special constexpr allocators, making compile-time strings/vectors incompatible with their normal equivalents? 回答1: Those two papers depend heavily on P0784, which discusses how allocations at compile-time will work. Incomplete answer: Only std::allocator will work. All allocations are tracked, and must be deallocated

How will C++20 constexpr containers work?

僤鯓⒐⒋嵵緔 提交于 2020-12-25 04:10:12
问题 As constexpr std::string and constexpr std::vector have been accepted into C++20, how will these be used? The linked papers are very short on details. Do we need to specify special constexpr allocators, making compile-time strings/vectors incompatible with their normal equivalents? 回答1: Those two papers depend heavily on P0784, which discusses how allocations at compile-time will work. Incomplete answer: Only std::allocator will work. All allocations are tracked, and must be deallocated