C++
Awkward passing of Allocators and Schedulers to C++20 Coroutines
C++20 coroutines are here and give us some really intersting functionality. When working on library code I've encountered inelegance in the following: PMR allocators to a coroutine not practical because you cannot access arguments provided to coroutines. Seems like get_return_object could be provided with the function arguments, anybody see problems with that? Passing ordinary allocators to coroutines constructable just like containers:
[link] [comments]
Awkward passing of Allocators and Schedulers to C++20 Coroutines
C++20 coroutines are here and give us some really intersting functionality. When working on library code I've encountered inelegance in the following: PMR allocators to a coroutine not practical because you cannot access arguments provided to coroutines. Seems like get_return_object could be provided with the function arguments, anybody see problems with that? Passing ordinary allocators to coroutines constructable just like containers:
constexpr vector( size_type count, const T& value, const Allocator& alloc = Allocator()); Passing various worker queues to different coroutines without singleton or global data. I'm interested in making a proposal to resolve some of my concerns with coroutines.Getting opinions from the community is really important to me. Finding out I'm wrong about a problem is even more important... Hope people don't mind a few of these related posts. Are others finding the same issue as me?Is this something people want? I'll keep adding to this list: Idea 1: Provide function arguments to get_return_object Pros: Could grab an allocator/queue/whatever from here. Could use this for logging and instrumentation. Could just be an additional overload avaliable for get_return_object. Cons: Provided arguments could be mutated, this might lead to confusing situations. submitted by /u/Seppeon [link] [comments]
Modernes C++ - ModernesCpp.com
C++20: Extend std::format for User-Defined Types
Peter Gottschling presented in his last post "std::format in C++20" the basics to the new formatting library in C++20. In today's post Peter writes about the formatting of user-defined types.
C++20: Extend std::format for User-Defined Types
Peter Gottschling presented in his last post "std::format in C++20" the basics to the new formatting library in C++20. In today's post Peter writes about the formatting of user-defined types.
Bartek's coding blog
Increased Complexity of C++20 Range Algorithms Declarations - Is It Worth?
With the addition of Ranges and Concepts in C++20, our good old algorithm interfaces got super long “rangified” versions. For example,
Increased Complexity of C++20 Range Algorithms Declarations - Is It Worth?
With the addition of Ranges and Concepts in C++20, our good old algorithm interfaces got super long “rangified” versions. For example,
copy is now 4 lines long… and it’s just the declaration! template std::weakly_incrementable O>
requires std::indirectly_copyable, O>
constexpr ranges::copy_result, O>
copy(R&& r, O result); How to decipher such a long declaration? What benefits do we get instead? Is it worth it? Let’s find out. Read more...C++
std::i/ostream's are a bit too oversized for both usage and implementation. Are there lighter alternatives widely used?
Disclaimer: I'm not solving any specific production case. Standard input/output stream specifications include a lot of details - locales, buffering, large set of virtual functions. And they're known for their performance implications. So I was wondering if there's some simpler version of i/o interface, coming from some 3rd party library, which is widely used. Something like this:
[link] [comments]
std::i/ostream's are a bit too oversized for both usage and implementation. Are there lighter alternatives widely used?
Disclaimer: I'm not solving any specific production case. Standard input/output stream specifications include a lot of details - locales, buffering, large set of virtual functions. And they're known for their performance implications. So I was wondering if there's some simpler version of i/o interface, coming from some 3rd party library, which is widely used. Something like this:
struct reader { virtual std::size_t read(std::span buffer) = 0; } struct writer { virtual std::size_t write(std::span buffer) = 0; virtual void flush() = 0; } Or does everyone either just use std::i/o stream's or reimplements concept above from scratch? submitted by /u/target-san [link] [comments]
C++
bitflags v1.3.0 - raw flags support
Hi everybody, New version of bitflags library is released these days! Previously, you could create your flags only by writing something like:
[link] [comments]
bitflags v1.3.0 - raw flags support
Hi everybody, New version of bitflags library is released these days! Previously, you could create your flags only by writing something like:
BEGIN_BITFLAGS(Flags) FLAG(none) FLAG(flag_a) FLAG(flag_b) FLAG(flag_c) END_BITFLAGS(Flags) Each flag is consisted of raw value (bits) and flag's name. So you could get a string representation of each flag by writing: Flags::flag_a.name . In the new version, you can create raw flags, i.e. flags without string representation. Purpose of this feature is to provide light version that would take up as little space as possible: BEGIN_RAW_BITFLAGS(RawFlags) RAW_FLAG(none) RAW_FLAG(flag_a) RAW_FLAG(flag_b) END_RAW_BITFLAGS(RawFlags) What do you think of this feature? What's next? In the next version, support for C++11 and C++14 will be added. Thank you all! submitted by /u/mNutCracker [link] [comments]
C++
Modern C++ Undo/Redo framework
Hello everyone, just sharing my latest development.
https://github.com/ryder052/History Happy coding :) submitted by /u/Ryder052
[link] [comments]
Modern C++ Undo/Redo framework
Hello everyone, just sharing my latest development.
https://github.com/ryder052/History Happy coding :) submitted by /u/Ryder052
[link] [comments]
C++
When a coroutine ‘suspends’, does control flow halt or pass to the caller?
https://github.com/lewissbaker/cppcoro/blob/master/lib/async_manual_reset_event.cpp I’m trying to understand what happens when await_suspend returns true. Does the thread of execution pass control back to where co_await was called by the user? Or does the thread of execution simply halt until we get a call to resume? Put another way, after await_suspend returns true, where does the EIP register point? Are CPU resources returned to the OS? After iterating through the linked list on a call to set(), how does resume() know which threads of execution to resume on? Does the coroutine handle hold this information? submitted by /u/XiPingTing
[link] [comments]
When a coroutine ‘suspends’, does control flow halt or pass to the caller?
https://github.com/lewissbaker/cppcoro/blob/master/lib/async_manual_reset_event.cpp I’m trying to understand what happens when await_suspend returns true. Does the thread of execution pass control back to where co_await was called by the user? Or does the thread of execution simply halt until we get a call to resume? Put another way, after await_suspend returns true, where does the EIP register point? Are CPU resources returned to the OS? After iterating through the linked list on a call to set(), how does resume() know which threads of execution to resume on? Does the coroutine handle hold this information? submitted by /u/XiPingTing
[link] [comments]
C++ – Типизированный язык программирования
Обидно за мнения про статические анализаторы кода
Инструменты статического анализа кода ушли далеко вперёд. Это вовсе не те "линтеры", которые активно применялись 20 лет тому назад. Однако многие по-прежнему относятся к ним, как к очень простым инструментам. Обидно. Обидно, как за методологию анализа кода в целом, так и за инструмент PVS-Studio.
Читать дальше →
Обидно за мнения про статические анализаторы кода
Инструменты статического анализа кода ушли далеко вперёд. Это вовсе не те "линтеры", которые активно применялись 20 лет тому назад. Однако многие по-прежнему относятся к ним, как к очень простым инструментам. Обидно. Обидно, как за методологию анализа кода в целом, так и за инструмент PVS-Studio.
Читать дальше →
Jason Turner (Youtube)
C++ Weekly - Ep 241 - Using `explicit` to Find Expensive Accidental Copies
Support this channel and learn something new; buy my C++ Best Practices Book! https://leanpub.com/cppbestpractices
My Training Classes: https://emptycrate.com/training.html
Support these videos: https://www.patreon.com/lefticus
Follow me on twitter: https://twitter.com/lefticus
C++ Weekly - Ep 241 - Using `explicit` to Find Expensive Accidental Copies
Support this channel and learn something new; buy my C++ Best Practices Book! https://leanpub.com/cppbestpractices
My Training Classes: https://emptycrate.com/training.html
Support these videos: https://www.patreon.com/lefticus
Follow me on twitter: https://twitter.com/lefticus
Arthur O’Dwyer
Left-folding and right-folding an arbitrary callable
Nerdsnipe: Given an arbitrary binary callable
evaluates to
That is, we’re looking to define
This is easy with “recursive templates,” but in modern C++ we know that
“Iteration is better than recursion” (2018-07-23), so let’s try to
do it with C++17 fold-expressions. Godbolt:
There are a couple of tricks hiding in here:
The
I normally use
places where the type
The
I’m using it here only to capture
any standard library headers. I want to capture
it’s a move-only lambda type.
All of the uses of
and seeing what goes wrong in folding
We use aggregate initialization for
value of
rather than constructing a temporary and then having to move it into place.
This allows us to work with non-move-constructible types.
However, if the overall result of the fold is a prvalue of a
non-move-constructible type, we fail (Godbolt).
I can partially fix
but (1) I cannot fix
and (3) my change to
pathological cases. (Godbolt.)
Left-folding and right-folding an arbitrary callable
Nerdsnipe: Given an arbitrary binary callable
f(x,y), write a function left_fold such that left_fold(f)(a,b,c,...)evaluates to
f(f(f(a,b),c),...). Write a function right_fold such that right_fold(f)(a,b,c,...) evaluates tof(a, f(b, f(c, ...))).That is, we’re looking to define
left_fold and right_fold such thatauto leftsub = left_fold(std::minus<>);
auto rightsub = right_fold(std::minus<>);
static_assert(leftsub(1, 2, 3) == (1 - 2) - 3);
static_assert(rightsub(1, 2, 3) == 1 - (2 - 3));
This is easy with “recursive templates,” but in modern C++ we know that
“Iteration is better than recursion” (2018-07-23), so let’s try to
do it with C++17 fold-expressions. Godbolt:
#define FWD(x) static_cast(x)
#define MOVE(x) static_cast(x)
template
struct Folder {
const F& foo_;
TRR value_;
template
constexpr auto operator+(Folder&& rhs) && -> decltype(auto) {
using R = decltype(foo_(FWD(value_), FWD(rhs.value_)));
return Folder{foo_, foo_(FWD(value_), FWD(rhs.value_))};
}
};
template
constexpr auto left_fold(F foo) {
return [foo = MOVE(foo)](auto&&... args) -> decltype(auto) {
return (... + Folder{foo, FWD(args)}).value_;
};
}
template
constexpr auto right_fold(F foo) {
return [foo = MOVE(foo)](auto&&... args) -> decltype(auto) {
return (Folder{foo, FWD(args)} + ...).value_;
};
}
There are a couple of tricks hiding in here:
The
FWD(x) macro is just a shorter spelling of std::forward(x).I normally use
static_cast(x), but in this case I have severalplaces where the type
X is not easily nameable.The
MOVE(x) macro is just a shorter spelling of std::move(x), andI’m using it here only to capture
foo by move without having to includeany standard library headers. I want to capture
foo by move, just in caseit’s a move-only lambda type.
All of the uses of
-> decltype(auto) are necessary. Try removing themand seeing what goes wrong in folding
<< over (std::cout, 1, 2, 3).We use aggregate initialization for
Folder{...}, so that thevalue of
foo_(FWD(value_), FWD(rhs.value_)) will be constructed in-placerather than constructing a temporary and then having to move it into place.
This allows us to work with non-move-constructible types.
However, if the overall result of the fold is a prvalue of a
non-move-constructible type, we fail (Godbolt).
I can partially fix
left_fold’s issue with non-move-constructible types,but (1) I cannot fix
right_fold; (2) I cannot totally fix left_fold;and (3) my change to
left_fold causes additional failures in even morepathological cases. (Godbolt.)
template
struct LeftFolder {
const F& foo_;
TRR value_;
template
constexpr friend auto operator+(U&& lhs, LeftFolder&& rhs) -> decltype(auto) {
return rhs.foo_(FWD(lhs), FWD(rhs.value_));
}
};
template
constexpr auto left_fold(F foo) {
return [foo = MOVE(foo)](auto&& init, auto&&... args) -> decltype(auto) {
return (FWD(init) + ... + LeftFolder{foo, FWD(args)});
};
}Standard C++ (Twitter)
C++ Compile time conditional struct member variables--Saleem Ahmad bit.ly/3dmYm6J #cpp
C++ Compile time conditional struct member variables--Saleem Ahmad bit.ly/3dmYm6J #cpp
Standard C++ (Twitter)
C++ as a Second Language (Chrome University 2020)--Chris Blume bit.ly/3j0rUbT #cpp
C++ as a Second Language (Chrome University 2020)--Chris Blume bit.ly/3j0rUbT #cpp
Standard C++ (Twitter)
Increased Complexity of C++20 Range Algorithms Declarations - Is It Worth?--Bartlomiej Filipek bit.ly/2GJXOfE #cpp
Increased Complexity of C++20 Range Algorithms Declarations - Is It Worth?--Bartlomiej Filipek bit.ly/2GJXOfE #cpp