c++17

Access std::variant inside lambda

时间秒杀一切 提交于 2020-06-01 05:49:50
问题 I have the following code: std::variant<std::uint32_t, std::uint16_t, float> my_variant; template <typename T> void my_sub_func(T& value) { // do stuff }; void my_func(my_variant& value) { [&]() { my_sub_func(std::get<std::uint32_t>(value)); }; }; int main() { my_variant t; my_func(t); } I always get an Unexpected index exception at runtime, why is that and how can I avoid it? 来源: https://stackoverflow.com/questions/61930744/access-stdvariant-inside-lambda

How to understand the sentence “full-expression must be a constant expression” which mentioned in the standard

大城市里の小女人 提交于 2020-05-30 08:27:07
问题 Ago, Some rules in the standard that say they are applied to expression , I was confused about whether these rules can also be applied to full-expression arbitrarily. I get an answers in that question. However, there are some rules like "the full-expression of the initialization shall be a constant expression. " in the standard. Such as: dcl.constexpr#9 basic.start.static#2 They all say that the full-expression must be a constant expression in these above links. The prior condition for a

Linkage of function declared as `extern` in block scope according to the C++17 standard draft

时光毁灭记忆、已成空白 提交于 2020-05-29 06:08:10
问题 From the C++17 Standard Draft § 3.5.6 : The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage. If there is a visible declaration of an entity with linkage having the same name and type , ignoring entities declared outside the innermost enclosing namespace scope, the block scope declaration declares that same entity and receives the linkage of the previous declaration. If there is more than one such matching entity,

std::string_view compile time hashing

做~自己de王妃 提交于 2020-05-27 06:27:04
问题 It seems the std::hash functions for the C++17 string_view are not constexpr's. It seems to me that a string view bound to a const char[] could be hashed at compile time (which would be very sweet), or is there anything which prevents this? 回答1: Since C++14 (see 17.6.3.4 Hash requirements, table 26), we have: The value returned shall depend only on the argument k for the duration of the program. [Note: Thus all evaluations of the expression h(k) with the same value for k yield the same result

std::string_view compile time hashing

落爺英雄遲暮 提交于 2020-05-27 06:26:32
问题 It seems the std::hash functions for the C++17 string_view are not constexpr's. It seems to me that a string view bound to a const char[] could be hashed at compile time (which would be very sweet), or is there anything which prevents this? 回答1: Since C++14 (see 17.6.3.4 Hash requirements, table 26), we have: The value returned shall depend only on the argument k for the duration of the program. [Note: Thus all evaluations of the expression h(k) with the same value for k yield the same result

C++17 atomics and condition_variable deadlock

浪子不回头ぞ 提交于 2020-05-26 12:24:22
问题 I have the following code, which deadlocks on the commented lines. Basically f1 and f2 run as individual threads in the program. f1 expects i to be 1 and decrements it, notifying the cv. f2 expects i to be 0 and increments it, notifying the cv. I assume the deadlock occurs if f2 increments i to 1, calls cv.notify(), then f1 reads a stale value of i (which is 0) because there is no memory synchronization between the mutex and i and then waits and never gets woken up. Then f2 also enters a

C++ How to insert a consecutive inter range into std::vector?

独自空忆成欢 提交于 2020-05-25 17:54:25
问题 Say I want all numbers from 23 to 57 be in a vector . I can do this: vector<int> result; for (int i = 23; i <= 57; ++i) { result.push_back(i); } But this is a 5 line solution for a simple job. Can't I do that more elegantly? The best syntax would be vector<int> result{23 .. 57}; for example or such a trivial one line code. Any options with C++17? 回答1: You can use std::iota (since C++11). Fills the range [first, last) with sequentially increasing values, starting with value and repetitively

C++ How to insert a consecutive inter range into std::vector?

怎甘沉沦 提交于 2020-05-25 17:53:15
问题 Say I want all numbers from 23 to 57 be in a vector . I can do this: vector<int> result; for (int i = 23; i <= 57; ++i) { result.push_back(i); } But this is a 5 line solution for a simple job. Can't I do that more elegantly? The best syntax would be vector<int> result{23 .. 57}; for example or such a trivial one line code. Any options with C++17? 回答1: You can use std::iota (since C++11). Fills the range [first, last) with sequentially increasing values, starting with value and repetitively

C++ How to insert a consecutive inter range into std::vector?

让人想犯罪 __ 提交于 2020-05-25 17:52:02
问题 Say I want all numbers from 23 to 57 be in a vector . I can do this: vector<int> result; for (int i = 23; i <= 57; ++i) { result.push_back(i); } But this is a 5 line solution for a simple job. Can't I do that more elegantly? The best syntax would be vector<int> result{23 .. 57}; for example or such a trivial one line code. Any options with C++17? 回答1: You can use std::iota (since C++11). Fills the range [first, last) with sequentially increasing values, starting with value and repetitively

C++ How to insert a consecutive inter range into std::vector?

送分小仙女□ 提交于 2020-05-25 17:51:08
问题 Say I want all numbers from 23 to 57 be in a vector . I can do this: vector<int> result; for (int i = 23; i <= 57; ++i) { result.push_back(i); } But this is a 5 line solution for a simple job. Can't I do that more elegantly? The best syntax would be vector<int> result{23 .. 57}; for example or such a trivial one line code. Any options with C++17? 回答1: You can use std::iota (since C++11). Fills the range [first, last) with sequentially increasing values, starting with value and repetitively