constexpr

Why does `auto` not adopt the constexpr'ness of its initializing expression?

孤人 提交于 2021-02-20 04:23:05
问题 Why doesn't defining a variable with auto keyword carry the constexpr 'ness of the expression used to initialize it? As an example, consider the following code: #include <string_view> constexpr std::string_view f() { return "hello"; } static constexpr std::string_view g() { constexpr auto x = f(); // (*) return x.substr(1, 3); } int foo() { return g().length(); } With GCC 10.2 and --std=c++20 -fsanitize=undefined -O3 , this compiles into: foo(): mov eax, 3 ret But if we remove the constexpr

How to initialize a constexpr std::array with templated constexpr member functions?

走远了吗. 提交于 2021-02-19 08:45:38
问题 This is a follow up of my question given here. In the end I want to create a constexpr std::array containing text with an appended running index. I wanted to try a different approach than in the previous question. Nearly everything, what I do in the below code is constexpr. But maybe, it is simply the old problem of returning a pointer to a no longer existing variable. But, I doubt this. Please see the following code, where the not working line in function main is marked. #include <iostream>

Compile-time template `std::integral_constant` counter - how to implement it?

守給你的承諾、 提交于 2021-02-18 23:00:47
问题 I have several types and I want to "bind" an std::integral_constant sequential ID value to every type at compile-time. Example: struct Type00 { }; struct Type01 { }; struct Type02 { }; struct Type03 { }; struct TypeXX { }; struct TypeYY { }; template<typename T> struct TypeInfo { using Id = std::integral_constant<int, ???>; }; int main() { cout << TypeInfo<Type00>::Id::value; // Should always print 0 cout << TypeInfo<Type01>::Id::value; // Should always print 1 cout << TypeInfo<Type02>::Id:

Weird behaviour constexpr with std::initializer_list

末鹿安然 提交于 2021-02-18 23:00:34
问题 I am trying to understand why the compiler is complaining here: // cexpr_test.cpp #include <initializer_list> constexpr int test_cexpr(std::initializer_list<const char*> x) { return (int) (*x.begin())[0]; // ensuring the value isn't optimized out. } int main() { constexpr int r1 = test_cexpr({ "why does this work," }); constexpr std::initializer_list<const char*> broken { "but this doesn't?" }; constexpr int r2 = test_cexpr(broken); return r1 + r2; } The message produced when compiled with g+

Weird behaviour constexpr with std::initializer_list

大城市里の小女人 提交于 2021-02-18 23:00:13
问题 I am trying to understand why the compiler is complaining here: // cexpr_test.cpp #include <initializer_list> constexpr int test_cexpr(std::initializer_list<const char*> x) { return (int) (*x.begin())[0]; // ensuring the value isn't optimized out. } int main() { constexpr int r1 = test_cexpr({ "why does this work," }); constexpr std::initializer_list<const char*> broken { "but this doesn't?" }; constexpr int r2 = test_cexpr(broken); return r1 + r2; } The message produced when compiled with g+

Is “if constexpr” useful outside of templates?

时光怂恿深爱的人放手 提交于 2021-02-18 22:51:52
问题 I'm trying to understand if constexpr fully. I understand, that if if constexpr(expr) used in a template, and expr is dependent on a template parameter, then during instantiation, only one of the then / else branches will be instantiated, the other will be discarded. I've got two questions: Is it true, that if expr is not dependent on a template parameter, then no branches of if constexpr(expr) will be discarded? If yes, where does the standard say so? I don't see where the standard has the

decltype( constexpr variable)

女生的网名这么多〃 提交于 2021-02-16 16:13:27
问题 Why decltype of constexpr variable is failed ? #include <cstdint> #include <type_traits> constexpr uint16_t foo(){ return 0;} constexpr auto cv = foo(); auto v = foo(); static_assert( std::is_same< uint16_t, decltype(cv)>::value, "!"); // failed static_assert( std::is_same< uint16_t, decltype(v) >::value, "!"); // success 回答1: decltype(entity) specifies the declared type of the entity specified by this expression. Due to the constexpr, ( A constexpr specifier used in an object declaration

decltype( constexpr variable)

依然范特西╮ 提交于 2021-02-16 16:13:00
问题 Why decltype of constexpr variable is failed ? #include <cstdint> #include <type_traits> constexpr uint16_t foo(){ return 0;} constexpr auto cv = foo(); auto v = foo(); static_assert( std::is_same< uint16_t, decltype(cv)>::value, "!"); // failed static_assert( std::is_same< uint16_t, decltype(v) >::value, "!"); // success 回答1: decltype(entity) specifies the declared type of the entity specified by this expression. Due to the constexpr, ( A constexpr specifier used in an object declaration

creating an std::array with size calculated during run time

这一生的挚爱 提交于 2021-02-16 14:03:58
问题 I want to create an object of std::array<T, N> but the problem is I can only use functions that return a constexpr type or compiler will complain. The problem here is that I need to calculate the length of this array based on another array's size which could be something like this: template <typename T> struct DataLength { template <typename iter> size_t maxPossibleLength(iter begin, iter end) { size_t m_size = 0; while (begin != end) { m_size = m_size << 8 | std::numeric_limits<T>::max(); /*

constexpr construction of a POD struct with a char[] field

偶尔善良 提交于 2021-02-11 15:52:06
问题 How can I make the function below constexpr ? This is a function that I use to create a POD struct that is defined in a "C" header file that I cannot change. I ended up with the following static function helper (which works) to make these structs, I believe it is not constexpr , as the strncpy call violates constexpr . static auto makeWPTEntry( const char* name, double aLatDeg, double aLatMin, double aLonDeg, double aLonMin, char bSeven = false, double aCourse = 0, double aAzimuth = 0, double