« first day (4072 days earlier)      last day (858 days later) » 

4:24 AM
 
 
4 hours later…
8:06 AM
cmak eneeds block comments
 
 
5 hours later…
nwp
1:32 PM
Somehow being able to do extern struct S {} s; makes me happy. And inline struct S{} s; is even better.
Though I suppose you should move the extern/inline before s.
Look at me making an attempt do write clear code 🤡
 
By adding more inline? How does that make code clearer?
 
nwp
Not more inline, moved inline.
struct S{} inline s;
It's clearer because inline refers to s and not S. Someone might wonder what the hell an inline struct is otherwise.
I can't even tell anymore if this is very obscure or just regular C++. The extern version is arguably regular C.
Sadly after going over the code again I have to remove it.
 
 
3 hours later…
5:09 PM
@TelKitty inline is like violence. The solution to too much is to do even more of it.
 
I just today tried to abuse the linker to provide fallback symbols in case some static lib is not linked.
I failed, ended up with some macro defines and and inline function.
That code is ugly and can easily lead to an odr violation, but so would the weird linker shenanigans I guess
 
@PeterT Yeah--what you're looking for is a weak external, but in that area documentation generally runs somewhere between terrible and nonexistent.
 
yeah stumbled across that too, but it seemed to not consistently work with static libs across compilers, or at least not with the same methods
 
@PeterT You have to adopt the attitude you would toward a dog walking on its hind legs. Don't be disappointed that it works poorly. Just be happy that it sometimes sort of works at all...
 
because if I wander into "oh, it's not documented anywhere, I just found it perusing the msvcrt internals", I should turn around.
 
5:20 PM
@PeterT Yeah, probably.
 
5:50 PM
@JerryCoffin In C++ the global operator new() is like a weak symbol. If you don't define it then a default version is used. Kinda funny that this is a special case, in the normal case you'd get a duplicate symbol error.
Would have been nice if they made it a language feature.
 
which is what the undocumented functionality of the linker was used for in msvcrt
 
It would be nice if weak symbols were supported as an official C++ feature. It could be handy.
 
it would've helped me here, but I'd honestly say "please don't" at this point.
 
6:39 PM
@StackedCrooked It could be, but I'm not convinced it would be often enough to justify the time spent on standardizing it, when I think in terms of other features that would be delayed to work on this instead.
 
Just saying that it's not nice to rely on compiler extensions. And with operator new() there's already a precedent. They might as well have made it a feature instead of going with a special case.
 
@StackedCrooked Oh, I'm not saying it wouldn't be nice--only that it doesn't immediately strike me as obvious that it would be used enough to justify delaying other things to do it.
 
Does C++ really need more features? I could have died happy after C++11.
Shorter lambda syntax would be nice, though...
 
@fredoverflow Dunno how they could have made it even shorter than it already is..
 
nwp
They could have allowed an expression instead of a block.
 
6:52 PM
C++  [](auto&& a, auto&& b) { return a + b; }
Java (a, b) -> a + b
 
OK
@fredoverflow I kinda agree though. C++11 was very exciting. The only obviously missing features were make_unique and lambda move capture, both were added to C++14.
 
nwp
A dynamic 2D array in the standard library would be nice.
 
@nwp How would that be different from std::vector<std::vector<X>>?
 
I suppose more features for atomics is also desirable (like latch, etc...)
 
nwp
It would be more efficient because it would use a single std::vector underneath. Also you could resize it without having to write a loop and resizing every inner vector. Also you wouldn't get into issues where you end up with a vector of differently sized vectors, but that one is minor I guess.
std::vector<std::vector> is generally seen as an anti-pattern to be avoided.
 
6:56 PM
@nwp Citation needed
 
It's pretty easy to craft your own Grid class.
@fredoverflow Wow, such authority :P
For my little Tetris project I also implemented a Grid class using a 1-dimensional vector internally.
 
Tetris grids aren't supposed to grow when they get full though ;)
 
Yeah, it was fixed size.
 
Why the vector then?
 
7:03 PM
What's the alternative?
 
nwp
@StackedCrooked Beginners keep asking how to do that and there is just no good answer. Them doing it themselves just makes them give up on C++ because it's extremely verbose, using some library like Eigen is a pain as well and in the end they just get stuck in this stupid detail when they wanted to do something else.
 
@fredoverflow OK, I suppose the alternative today is std::unique_ptr<int[]>, but my little Tetris project was before C++11.
 
std::array or C array with fixed size?
 
In my case the grid size dimensions are determined at runtime.
So I can't use std::array.
 
oh
How about std::array<Cell, 1234567890> then, and hoping the the user never needs more, in the spirit of C? ;)
 
7:07 PM
I mean, that could work as long as I don't initialize the unused storage. The OS won't allocate pages for unused memory :D
I never considered that, how stupid of me :)
 
nwp
std::array<std::array<Cell, 1234567890>, 1234567890> for 2D
How nice that you can pass that by value and it doesn't decay to a pointer like in C :D
 
lol
1032 years later...
 
7:32 PM
@nwp isn't mdspan all but in for 2023?
 
nwp
I have no idea what that is. I barely know std::span.
Multi-dimensional span I guess.
If that could do the index math while being backed by an std::vector that would be great.
 
yep, that's what it's for
 
nwp
Then we just need to wait 5 years until we can reasonably expect people to have a compiler that supports it :D
 
I think it grew out of the audio proposal of all things, because they wanted a better structure to feed interleaved multi-channel audio to buffers
but yeah it would be great to have something reasonable to represent image buffers and stuff like that with std-lib data types.
 
8:09 PM
Is C++ dying? I mean C++11 was a great revival, but the enthusiasm gradually went down after C++14. I don't even find C++20 to be very exciting despite the major new features introduced like concepts, modules, coroutines and ranges. Meanwhile Rust is becoming an accepted language to implement Linux drivers. It feels as if C++ has no future...
 
IGP
@StackedCrooked I hope not. I can finally understand its syntax!
 
@IGP Wait till you hear about pointers to member functions :)
 
IGP
I said I understand its syntax
 
That implies you're a C++ expert.
 
IGP
Is that all it takes to be a C++ expert in this day and age? xD
 
8:16 PM
@IGP I was gonna point out that typo :)
 
IGP
Too slow.
 
@IGP I'm generous.
@IGP Deduction guides?
 
IGP
@StackedCrooked I'm not sure what you mean by that.
 
I was just testing your knowledge of C++ syntax.
Don't worry, I also have never used this.
 
IGP
I've barely scratched the surface of the stl
 
8:20 PM
My point is that claiming to understand C++ syntax means claiming to be a C++ expert.
Anyway, no hard feelings :)
But the fact remains that C++ is becoming increasingly hard to master.
 
IGP
I think there's something wrong with those 2 statements.
I would think mastering a language comes well AFTER you understand its syntax.
 
And C++ has been declining, I believe. In the late 90s it lost some market share to Java. Later it lost some to C#. Now there's Go and Rust...
 
IGP
So C++ is becoming increasingly hard to learn
 
Yeah, it's harder to learn now, despite the new features that simplify its usage, it's still harder to learn, because there's more to learn.
 
IGP
Then again, sometimes you only need the tip of the iceberg
 
8:25 PM
That applies to most programming languages.
But not to C++ in my opinion.
 
@StackedCrooked You can also use std::vector and call reserve()
 
IGP
Why not?
I mean, it depends what you're using C++ for
 
You really need a solid understanding of the basics, and even learning the basics is an intensive 3 month course with a good teacher in the ideal situation.
 
@StackedCrooked I do not know Rust, but learning the borrow checker does not seem so easy after all.
 
@MangaD Please use the "arrow" to answer to specific messages :)
 
IGP
8:27 PM
All my jobs have been mostly PHP but I've only really needed to know the bare minimum of it. I don't know how to use most of its extensions.
 
Thank you for the tip, I am new here. :D
 
IGP
after the course I did, I feel I'm good with the basics. I know the syntax for pointers, classes, (simple) templates. If I ever need something more difficult I feel like I could just read up on the available documentation
 
IGP
@StackedCrooked The basics are the tip of the iceberg, no?
 
@IGP I'm supportive of you learning C++.
But it takes a long time.
 
8:34 PM
@StackedCrooked I see!
 
IGP
Does valgrind work with C++?
 
Yep, works great
 
I started in 2003 using the book "Accelerated C++", which was considered to be one of the best beginnner books by the C++ experts (I mean, the real experts that are part of the committee). Then I got a job at a decent company. After my first 5 years as a professional C++ developer I thought I knew it all. But then StackOverflow was founded. And I feel like I learned just as much during the first five years AFTER StackOverflow as I learned the five years before that.
 
ugg, you guys do much nix stuff?
 
@Mikhail 99% nix and 1% Windows.
 
IGP
8:40 PM
@StackedCrooked I mean, I'm not kidding myself. I know I'm barely a beginner. There were some instances while I was in college where I had to learn some C++ but I learned absolutely nothing. Just #include <iostream> and cout << 'Hi' << endl; I'm not even kidding.
 
@IGP That's how we all start.
 
IGP
Then, much later and after I graduated knowing the bare minimum, I read a BAD C++book
 
@IGP Yes, valgrind works awesomely great with C++.
 
@StackedCrooked really for C++?
do you just use nix to provision the dependencies and then use nix-shell to run a cmake?
 
IGP
Something about developing games with C++. I can't recall the actual title. The book never once touched upon GUI. It was all practice, no theory. Concepts thrown about without any explanation.
 
8:42 PM
@IGP Don't you read book reviews? In 2003 I read "Accelerated C++" because it got great reviews on Amazon, yes, back then Amazon book reviews were very reliable...
 
IGP
Not at that time!
 
@Mikhail It used to be different at my previous job.
 
IGP
And finally, I decided to take advantage of my company 's offer to get me a few free udemy courses
 
@Mikhail But most of my development time is for a server-side app that runs on Linux.
 
IGP
I went for the longest "beginner" c++ course and here I am.
 
8:46 PM
Are you writing C++ code for your job?
If you do, and if you have more experienced colleages, then you'll learn fast.
 
udemy is garbage
 
IGP
Nope, unfortunately. I think I'll have to do solo projects and pay auditors if I want to further my learning.
I did turn that course into 67 pages worth of handwritten notes.
@Mikhail It's a site selling diamonds when all it has is coal.
I already have an idea for my first c++ project, but it will have to wait until the year's over. I do have to complete another long course before new year's eve and that leaves me with precious little free time
 
@IGP Dunno about udemy. There are many bad courses out there. But even a bad course will help you understand the basic concepts about pointers and objects and stuff. The thing they don't teach you is the C++ idioms. So I recommend you to read "Effective C++" and "Effective Modern C++", or if that is too much, then at least peruse through the C++ Core Guidelines.
 
IGP
I'm not sure what you refer to by C++ idioms
 
You know the saying: "In Rome, do as Romans do"
That means something similar in programming languages.
Each language has its own style and habits of how to write code.
 
IGP
8:55 PM
So... quote unquote Best Practices? Popular and broadly adopted Conventions?
 
Yeah, that's basically it.
But it's not merely something superficial.
If you adhere to the idioms then you're less likely to run into problems.
 
IGP
Let me see if I understand what you refer to. Would you consider the following "idioms"?

1) Declare variables as close as possible to where you're going to use them.
2) For every allocation on the heap (new), make sure the memory is freed (delete)
 
Hm,..
1) is a good habit in any programming language
2) In C++ this there's "RAII"
@IGP Have you heard of RAII?
 
IGP
Resource Acquisition is Initialization
I'm sure I got the A wrong
But yeah, there was a whole section on it
 
@IGP I'm not judging you :)
 
9:00 PM
@StackedCrooked As far as I can see, Go isn't really playing the same game.
 
It is kind of emblematic of how there's probably never again going to languages as dominant as C, C++ or Java were for some time
 
@StackedCrooked RIAA: Recording Industry Association of America. Mostly known for the equalization curve they standardized that's applied to essentially all reasonably recent recordings on vinyl (in America or elsewhere).
 
There's so many more languages for every niche all the time
 
426
Q: Is C++ context-free or context-sensitive?

fredoverflowI often hear claims that C++ is a context-sensitive language. Take the following example: a b(c); Is this a variable definition or a function declaration? That depends on the meaning of the symbol c. If c is a variable, then a b(c); defines a variable named b of type a. It is directly initiali...

 
@IGP "Resource Acquisition is Initialization", it's one of the first idioms you must "grok". It's not that complicated I guess (But then again, you need to understand copy constructor, copy assignment operator, move constructors and move-assignment operator, so I suppose it's not trivial for a beginner...)
 
IGP
9:04 PM
grok
There you go again with the weird words
 
Grok is a common word used in reddit discussions. It means "to understand intuitively". (It's a word invented by a science fiction author, I believe...)
 
@StackedCrooked In current use, you mostly need to understand "don't use raw pointers".
 
IGP
@StackedCrooked The course I did went to great lengths to explain all of those.
 
@IGP Did the course mention something like the "rule of three" (or maybe even "rule of five", "rule of zero", or perhaps "rule of all or nothing")?
 
9:07 PM
@StackedCrooked Correct--by Robert Heinlein, to be specific.
 
IGP
@StackedCrooked I don't think so. That sounds like something I'd remember
 
@IGP I mean, it's great that you learned about that. But you also need to learn about when or when not to use them.
@IGP That's crucial. Here's a simple program that crashes before it exits. Can you figure out why?
@IGP Basically, there's an Array class that allocates memory in its constructor, and it frees the memory in its destructor. That's very appropriate. However, for some reason this simple program crashes...
 
IGP
Judging by the error message without reading the code, sounds like it's freeing twice.
Gimme a few minutes to go over the code
 
If you understand the rule-of-three you will immediately see the problem.
 
IGP
Copy constructor is called in the print array
 
9:13 PM
To be honest, I have used this question in job interviews. Nobody was able to spot the problem without me helping them.
 
IGP
the pointer in the Array<int> class gets copied, which means there are now 2 pointers pointing to the same location
 
@IGP Ok, but what's the cause of the crash?
 
@IGP You get a gold star.
 
ohhhhh
 
IGP
when the print function is done, the local copy goes out of scope
 
9:14 PM
You don't pass the array by reference to the print function
 
IGP
the local copy's destructor frees the same place in memory the original Array<int> is pointing to
so, when main is over, the destructor is called and bam, trying to delete something that was already deleted
 
@IGP Yes. Very nice.
@IGP Did your school teach you this?
 
IGP
college? heck no
the udemy course did
 
@MangaD Passing it by reference would eliminate the problem in this case, but the class would still be fragile and error prone (i.e., any copy would lead to the same problem). That said, yes, it probably should be passed by reference anyway.
 
9:16 PM
@IGP But they didn't teach you about rule-of-three?
 
IGP
No, no mantras were taught
Just the whys
And the "what happens if we don't"s
 
@IGP Btw, the fact you are reading the error message carefully means that you might have a bright future in software development :)
4
 
IGP
Yay
 
@IGP The rule-of-three is a simple guideline that will prevent these kind of errors. It says that if you define any of the following, then it should probably implement all three: (1) destructor (2) copy constructor (3) copy-assignment operator.
Using the idioms will prevent many common mistakes.
 
Then you have the rule of five and rule of zero. :^)
 
9:21 PM
Yeah.
 
IGP
@StackedCrooked Might as well implement the move constructor and move-assignment operator while we're at it.
 
Yes, since C++11 the situation has become a bit more complicated. (But the nice thing is that C++98 code that uses the rule-of-three will still work correctly in C++11.)
 
IGP
Something slightly similar but not at all related to memory management I remember the course taught was how important it was to overload the < and == operator if you ever wanted to use the STL algorithms in your own classes.
From what I remember, Move Constructor was just something to prevent having too much overhead caused by many copy constructor calls.
 
@IGP Yep, that's one thing. But then you can choose to implement those as member functions, or a free functions outside of the class, or even as friend functions inside the class definition. The last one is not commonly known but it's actually the best one.
 
IGP
@StackedCrooked The course reserved friend functions specifically for the overloaded stream insertion/extraction operators
 
9:26 PM
@StackedCrooked If you were to allocate the array using unique_ptr, then the destructor would not free the memory on the first run, am I right?
 
@IGP Yeah. Intuitively you can compare it to copying a file to another location versus moving a file. If you move the file it's much faster. However, moving the file removes the original, which is an important implication. Also, moving a file to another disk will actually invoke a copy followed by a delete of the original. So moving is usually faster, but not always. In C++ it's similar.
@MangaD If the array was stored in a unique_ptr, then the copy to the print function would fail and cause a compiler error. Which is a good thing, because it will prevent you from making this mistake..
 
@MangaD Your description would fit more closely with shared_ptr.
 
IGP
@StackedCrooked In the context of c++ memory management what would be the equivalent of moving something to another disk?
 
@IGP It's OK to use them to implement comparison operators. Friend functions have a bad reputation because they can access private member variables. But remember that normal member functions also have access to private member variables, and nobody seems to complain about that. If you implement operator== as a member variable then it has access to private members. So why would that suddenly be a problem if it's a friend function instead?
 
IGP
Yeah, the guy teaching the course pretty much said the same thing.
 
9:35 PM
In fact, it's not only okay, but often preferable to use friend functions for quite a few operators. For example, let's assume you define an Integer class. If you implement its operator+ as a member function, foo + 1 will work, but 1 + foo will fail. But if you implement it as a friend function (with an appropriate constructor) both will work as expected.
 
@IGP There's not really a good equivalent (perhaps moving an object to shared memory would be a nice equivalent.) But the thing to remember is that moving an object is sometimes faster and not always. And another point is that moving an object invalidates the original, so there's a semantic difference between move and copy, and you can't just replace copies with moves blindly without thinking about this consequence.
 
I tried your example with `shared_ptr`, but I am getting a runtime error:
malloc(): corrupted top size
Aborted (core dumped)
It works fine with unique_ptr though
 
IGP
@StackedCrooked Yeah, move constructor is not a replacement to copy constructor by any means.
 
@MangaD This gives a compiler error on coliru coliru.stacked-crooked.com/a/0cb5655399e3d7eb
 
What is wrong with make_shared?
 
IGP
9:44 PM
It is optional to declare one but should be declared when working with raw pointers, right? Also, there's the thing with temp values. Going back to the Integer class, maybe there's something like

Integer i;
i = Integer(2) + Integer(3); <- [1] (Integer(2)+Integer(3)) is evaluated and stored as a temp value. [2] Temp value is stored in i and then [3] it is discarded.
 
@MangaD Honestly, I'm not sure, but it seems like shared_ptr doesn't support arrays (unlike unique_ptr). (At least that's why I get from glancing at this post.)
 
@MangaD Nothing. But you want a shared_ptr<T>, not a shared_ptr<T[]>. coliru.stacked-crooked.com/a/c365e52897034e8a
 
IGP
@StackedCrooked shared pointers do not support managing arrays by default
 
@JerryCoffin That looks very wrong...
Just look at the output...
 
@StackedCrooked How curious...
 
9:46 PM
@StackedCrooked could easily be--I just modified it quickly without checking it over at all.
 
:)
@JerryCoffin You can't afford mistakes like this. I look up to you!
What if the Internet found out about this?
What if I tell the robot?
 
But according to that post, C++17 supports it, and yet, I get the runtime error:
malloc(): corrupted top size
Aborted (core dumped)
 
@StackedCrooked Go ahead and see. I've no problem with people noticing that I screw up now and again. :-)
 
Hm, I suppose I'll keep it secret for now so I can hold it against you for future blackmail.
 
IGP
*for future votes on the comittee
 
9:53 PM
@StackedCrooked Good luck with that. Would probably have worked better when I was young and single, so I thought of life as being about impressing people, especially pretty young ladies. Now I'm old and fat, so I rarely impress people all that much...
 
@MangaD Seems like this feature didn't make it to C++17 after all. At least I can find no documentation about it.
 
Apparently so. How very interesting. ^^
 
@JerryCoffin Nice try, I'm not conviced, haha :D
 
Actually, check this
In fact, taking into account your solution, the culprit is make_shared
 
Seems like shared_ptr<T[]> is supported after all, but it's not clear to me if you can create it with make_shared<T[]>...
 
IGP
10:02 PM
Weird.
 
@StackedCrooked It's supposed to. "Effects: Allocates memory for an object of type T (or U[N] when T is U[], where N is determined from args as specified by the concrete overload)."
 
Could it be a compiler bug?
 
Maybe, but I'm probably missing something obvious...
 

« first day (4072 days earlier)      last day (858 days later) »