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
C++
Taruga is a simple single-header C++11 library for turtle graphics!
Examples (on Streamable): drawing a Koch fractal and drawing a spiral. Differently than other C++ implementations, Taruga offers a very simple API, it's code is easy to understand, needs only one (widely accessible) dependency and is hardware-accelerated. More examples (and obviously, the code) are available at the Taruga repo. Any insights and (constructive) critique are welcome! submitted by /u/VinceMiguel
[link] [comments]
Taruga is a simple single-header C++11 library for turtle graphics!
Examples (on Streamable): drawing a Koch fractal and drawing a spiral. Differently than other C++ implementations, Taruga offers a very simple API, it's code is easy to understand, needs only one (widely accessible) dependency and is hardware-accelerated. More examples (and obviously, the code) are available at the Taruga repo. Any insights and (constructive) critique are welcome! submitted by /u/VinceMiguel
[link] [comments]
C++
C++ in a web app (series of blog posts)
Want to learn how to run your C++ code on the web? A series of blogs has been published to explain the steps to do this. Great for demonstration or to move the heavy computation from the server to the browser. Using C++ in a web app with WebAssembly Help! My C++ web app is not responding Interact with your C++ web app using React forms Spice up your C++ web app with visualizations C++ web app with WebAssembly, Vega, Web Worker and React submitted by /u/fdiblen
[link] [comments]
C++ in a web app (series of blog posts)
Want to learn how to run your C++ code on the web? A series of blogs has been published to explain the steps to do this. Great for demonstration or to move the heavy computation from the server to the browser. Using C++ in a web app with WebAssembly Help! My C++ web app is not responding Interact with your C++ web app using React forms Spice up your C++ web app with visualizations C++ web app with WebAssembly, Vega, Web Worker and React submitted by /u/fdiblen
[link] [comments]
Arthur O’Dwyer
Grab bag
“Why does man print gimme gimme gimme at 00:30?”
In 2010, artist Blake Fall-Conroy produced a “Minimum Wage Machine.”
The minimum wage machine allows anybody to work for minimum wage. For as long as they turn the crank,
the user is paid in pennies… For example, if minimum wage is $7.25/hour (the current US Federal rate),
then the worker is paid one penny every 4.97 seconds. If they stop turning the crank, they stop
receiving money.
The machine was originally programmed to dispense one penny every 4.97 seconds, corresponding to the
US federal minimum wage of $7.25 per hour ($14,500 per 2000-hour year) as of 2009.
A decade later, the federal minimum wage remains
$7.25 per hour ($14,500 per year).
If you haven’t seen One Pixel Wealth yet,
take a moment and see it. Serendipitously, the green square at the beginning represents
approximately 69 years’ worth of wages at $14,500 per year.
The California Republican Party has admitted
setting up rogue ballot boxes
and marking them as “official,” despite cease-and-desist letters from the California Secretary of State.
It’s not clear what the Republicans’ plan is, other than to sow confusion.
For an idea of what Republican voter intimidation has looked like in the past, take a look at
the Ballot Security Task Force,
a successful effort by the Republican National Committee to suppress the black vote in the
1981 New Jersey gubernatorial race.
The task force consisted of a group of armed off-duty police officers wearing armbands,
who were hired to patrol polling sites in African-American and Hispanic neighborhoods of Newark and Trenton.
BSTF patrols challenged and questioned voters at the polls and blocked the way of some prospective voters.
Roger Stone served as “chief strategist”
for the Republican gubernatorial candidate Thomas Kean, who ended up winning that race
thanks to the Ballot Security Task Force.
Earlier this year, Roger Stone was convicted of seven felonies stemming from the “Russiagate”
investigation; he was then summarily pardoned by Donald Trump.
Unprecedented, historic corruption:
an American president commutes the sentence of a person convicted by a jury of lying to shield
that very president.— Mitt Romney (@MittRomney) July 11, 2020
Most recently he’s been calling for Trump to declare martial law and
void the election results,
in a sort of belt-and-suspenders approach.
I’ll leave the last word to Republican Utah Senator and anti-masker Mike Lee:
We’re not a democracy.— Mike Lee (@SenMikeLee) October 8, 2020
Grab bag
“Why does man print gimme gimme gimme at 00:30?”
In 2010, artist Blake Fall-Conroy produced a “Minimum Wage Machine.”
The minimum wage machine allows anybody to work for minimum wage. For as long as they turn the crank,
the user is paid in pennies… For example, if minimum wage is $7.25/hour (the current US Federal rate),
then the worker is paid one penny every 4.97 seconds. If they stop turning the crank, they stop
receiving money.
The machine was originally programmed to dispense one penny every 4.97 seconds, corresponding to the
US federal minimum wage of $7.25 per hour ($14,500 per 2000-hour year) as of 2009.
A decade later, the federal minimum wage remains
$7.25 per hour ($14,500 per year).
If you haven’t seen One Pixel Wealth yet,
take a moment and see it. Serendipitously, the green square at the beginning represents
approximately 69 years’ worth of wages at $14,500 per year.
The California Republican Party has admitted
setting up rogue ballot boxes
and marking them as “official,” despite cease-and-desist letters from the California Secretary of State.
It’s not clear what the Republicans’ plan is, other than to sow confusion.
For an idea of what Republican voter intimidation has looked like in the past, take a look at
the Ballot Security Task Force,
a successful effort by the Republican National Committee to suppress the black vote in the
1981 New Jersey gubernatorial race.
The task force consisted of a group of armed off-duty police officers wearing armbands,
who were hired to patrol polling sites in African-American and Hispanic neighborhoods of Newark and Trenton.
BSTF patrols challenged and questioned voters at the polls and blocked the way of some prospective voters.
Roger Stone served as “chief strategist”
for the Republican gubernatorial candidate Thomas Kean, who ended up winning that race
thanks to the Ballot Security Task Force.
Earlier this year, Roger Stone was convicted of seven felonies stemming from the “Russiagate”
investigation; he was then summarily pardoned by Donald Trump.
Unprecedented, historic corruption:
an American president commutes the sentence of a person convicted by a jury of lying to shield
that very president.— Mitt Romney (@MittRomney) July 11, 2020
Most recently he’s been calling for Trump to declare martial law and
void the election results,
in a sort of belt-and-suspenders approach.
I’ll leave the last word to Republican Utah Senator and anti-masker Mike Lee:
We’re not a democracy.— Mike Lee (@SenMikeLee) October 8, 2020
C++
Question about packages coming from a Python Perspective
Okay, so I want to learn a little cpp, so I decided to do this facial recognition thing with OpenCV. I've done it before with python. Super easy, quick and simple. With Python you can just pip install the OpenCV module, and use it in seconds. However, when attempting with Cpp, it looks like I'm having to use this cmake command to build the OpenCV ordeal. This process it taking FOREVER. I am wondering if I am missing something or if this is just the process with cpp. I found out there isn't a pip for cpp unfortunetly. I guess what I am trying to figure out is: - Is this a normal application for cpp? (Facial Recognition) - Do you have to build each package you want to use? (ie OpenCV) - Is there some fundamental thing I am missing? submitted by /u/chancedare
[link] [comments]
Question about packages coming from a Python Perspective
Okay, so I want to learn a little cpp, so I decided to do this facial recognition thing with OpenCV. I've done it before with python. Super easy, quick and simple. With Python you can just pip install the OpenCV module, and use it in seconds. However, when attempting with Cpp, it looks like I'm having to use this cmake command to build the OpenCV ordeal. This process it taking FOREVER. I am wondering if I am missing something or if this is just the process with cpp. I found out there isn't a pip for cpp unfortunetly. I guess what I am trying to figure out is: - Is this a normal application for cpp? (Facial Recognition) - Do you have to build each package you want to use? (ie OpenCV) - Is there some fundamental thing I am missing? submitted by /u/chancedare
[link] [comments]
C++
Does this code snippet lead to undefined behavior?
I came across some code in android project https://cs.android.com/android/platform/superproject/+/master:system/netd/include/Fwmark.h;l=24?q=fwmark.h
[link] [comments]
Does this code snippet lead to undefined behavior?
I came across some code in android project https://cs.android.com/android/platform/superproject/+/master:system/netd/include/Fwmark.h;l=24?q=fwmark.h
union Fwmark { uint32_t intValue; struct { unsigned netId : 16; bool explicitlySelected : 1; bool protectedFromVpn : 1; Permission permission : 2; bool uidBillingDone : 1; }; constexpr Fwmark() : intValue(0) {} static inline uint32_t getUidBillingMask() { Fwmark m; m.uidBillingDone = true; return m.intValue; } }; The intention here is to use union to do cast between bitfield and uint32_t. I suspect this is still undefined behavior. submitted by /u/haohaolee [link] [comments]
C++
Networking in C++ Part #1: MMO Client/Server, ASIO & Framework Basics
https://www.youtube.com/watch?v=2hNdkYInj4g I found that video very intersting and how i hardly find any good content about networking programming with an use case in C++, i decided to share it here. submitted by /u/Dominus543
[link] [comments]
Networking in C++ Part #1: MMO Client/Server, ASIO & Framework Basics
https://www.youtube.com/watch?v=2hNdkYInj4g I found that video very intersting and how i hardly find any good content about networking programming with an use case in C++, i decided to share it here. submitted by /u/Dominus543
[link] [comments]
C++ (Rss)
Are there any good guides that directly compare MATLAB to C++ and vice versa?
I've been very happily working almost exclusively in MATLAB for a bit over ten years now. Initially, I used did controls, but then switched over to computational mechanics and geometry. I've only very sparsely used Python when collaborating with others, and have used C++ in a very minimal capacity (just quick edits of inputs and parameters). Due to the nature of what I'm working on these days, I've accepted that I just need to bite the bullet and learn C++ to a reasonable level of proficiency, and move my ongoing projects there. While tutorials are good and fine, I feel I would really benefit from a succinct comparison of MATLAB and C++ syntax, data structures, and general best practices; a translation between one and the other, if you will. Does anyone know of such a thing? All I've been able to find are mostly hollow blog posts with too much SEO. If anyone has any suggestions, thank you in advance! submitted by /u/send_me_ur_dog
[link] [comments]
Are there any good guides that directly compare MATLAB to C++ and vice versa?
I've been very happily working almost exclusively in MATLAB for a bit over ten years now. Initially, I used did controls, but then switched over to computational mechanics and geometry. I've only very sparsely used Python when collaborating with others, and have used C++ in a very minimal capacity (just quick edits of inputs and parameters). Due to the nature of what I'm working on these days, I've accepted that I just need to bite the bullet and learn C++ to a reasonable level of proficiency, and move my ongoing projects there. While tutorials are good and fine, I feel I would really benefit from a succinct comparison of MATLAB and C++ syntax, data structures, and general best practices; a translation between one and the other, if you will. Does anyone know of such a thing? All I've been able to find are mostly hollow blog posts with too much SEO. If anyone has any suggestions, thank you in advance! submitted by /u/send_me_ur_dog
[link] [comments]