« first day (1571 days earlier)      last day (1506 days later) » 

02:20
:50803336 My guess is that it works because it does not have to access any data inside derived, the function location in memory will be the same for every derived. If you had to access a Derived class member, things would probably be different.
02:57
@Vaillancourt But then I add a int a = 3; in Derived class and cout << a << endl; it in doBar(). No error again. It just gives me garbage value.
I expect/hope the program to crash, terminate in some ways
03:41
@Rick I suspect it would crash if you tried to get into the realm of addresses that have not been allocated to your program by the OS.
 
1 hour later…
04:54
ok
 
2 hours later…
06:24
Why do I see someone says that operators can not be virtual ?
 
2 hours later…
08:04
@PeterT but find() doesnt do insertion if it cant find the element?
@Permian correct, but in your if condition you always called the operator[] before calling find
yeah, you're still calling duplicates[p] before duplicates.find(p)
oh!
weird
still has an issue when i swapped it around
if ( duplicates.find(p) == duplicates.end() || duplicates[p] == d) {
im not sure that a pair is a hashable type
08:20
does it matter? you're not using an unordered_map, are you?
i am
unordered_map< pair<string,string>, int>
yeah its not hashable
the standard doesn't provide a hash implementation, I wouldn't say that counts as "not hashable"
boost::hash has an implementation for a hash function for std::pair
ok cool
but this seems like a weird choice to try and optimize with unordered_map and then still using std::string types to store single characters
i need to check the edges of [start, finish, dist] to find [start, finish, dist']
or [finish, start, dist']
its horrible the more you think about it
@PeterT no i only need the single characters to calculate a mapping between the strings to indices
08:38
right, but that implies that you will only ever have a single character in the string, if it's always sufficient to identify the node. So why would you use a type like std::string that is significantly larger than just a char and more complex to ccompare
at least you're not allocating more because of small string optimization, but I have a feeling that was not on purpose
because im given it in strings
and converting to chars proved to be difficult
i still have the pair issue with chars though (which is fixed)
I'm sure you can figure out an algorithm to "hash" two bytes from a pair of chars into an 8byte size_t
i got a hash function for that sort of thingnow
Hi, anyone uses Xcode for C++ here?
@Washery I do, mostly just for profiling and running some stuff
08:51
I see the only alternative is "command line"
huh? why is that. I use mostly VSCode to write the code
I am using dear imgui so it's not nice that when I open the executable a terminal window appears
I would think there's plenty of tools to generate a *.app package without having to use XCode to do the actual development.
ok
VSCode uses makefiles
well VSCode isn't really an IDE, it can use all kinds of build-systems
08:56
which means I'd have to write a makefile for the app
we use CMake to generate either Ninja or XCode project files
I see
have you ever used Dear ImGui?
for some quick experiments, yeah
from what I understand it's a real-time draw cycle
which means code within the main draw loop should be fast to execute
it provides an immediate mode api, that doesn't mean that it draws in immediate mode
it's not like immediate mode drawing is the most optimal way to do GPU drawing with todays architectures
09:01
what if I have to perform an action that takes some seconds? The GUI freezes
Let's say I need to invoke an API and show the result, I'd create a window using ImGui::Begin and ImGui::End, in between call the API and show the result using ImGui::Text()
You shouldn't do long-running tasks in the drawing thread, regardless of API. The same goes for JS in the browser, for OpenGL, for Win32 apps, for anything really
its totally not happy about char r1 = result[0]; claims there isnt the right type conversion
result[0][0] if result is the vector
you can't just convert a string of any length to a char implicitly
@PeterT that makes it happy!
do you mean using a separate thread?
09:05
either you do it in a separate thread, or you break up larger tasks.
it's been a while since I have played with threads in c++
std::async() should do the work right?
the API function returns a string, I can then check if the string is null in the draw thread (API request still running) or if not empty
if you use it correctly, yes. But it's easy to use it incorrectly (i.e. don't capture the returning futures)
auto handle = std::async(std::launch::async, parallel_sum, mid, end);
then handle.get() halts execution?
it waits for the result, yes
which blocks the draw thread
it doesn't make much sense to use threads in this way
09:14
@Washery you can still check the status of the future and only get the result when you know it's finished
Hey guys. I just wanted to know if teaching programming to a complete newbie (with 0 background in computers) by games is a good idea. I don't know of anyone who learnt to program by creating games, neither did I; that's why I ask :)
Whatever keeps peoples attention should be fine. There's plenty of people that started by writing small games in BASIC back in the day
Errm, these guys are gonna write video games using python
I mean if it's for starting to program it doesn't really matter, does it?
Hmm... I guess that's right. Thanks :)
But that won't be distracting for a 14 year old, will it?
09:25
distracting how? The most important part at that stage is just to keep at it.
That's like asking "will painting with felt pens slow down my kids artistic progression"
Hmm... Fine :)
 
2 hours later…
11:16
how would you find more than one shortest path in a graph?
or at least check whether there was more than one
you store that paths and not just sum the weights
in standard dijksta though i seem to be only able to store the shortest path it gives me
sure, that's what that algorithm is for
what so i need to generate all paths through the graph?
that seems cumbersome
you don't need all of them if you have the minimal one
then you can easily eliminate all the ones that are already higher than the minimal one
if the path isn't too long you could also increase each weight along the minimal path one after the other to get alternatives
or if you want to do it without recalculations you can just check all all weights of neighbours along the path
11:40
@PeterT this would end up being n! complexity
14
Q: Dijkstra's algorithm to find all the shortest paths possible

DarksodyI'm working on Dijkstra's algorithm, and I really need to find all the possible shortest paths, not just one. I'm using an adjacency matrix and I applied Dijkstra's algorithm, and I can find the shortest path. But I need to find all the paths with that minimum cost, I mean all the possible soluti...

somehow using previous nodes
only if every node along the way has N neighboring nodes
cant i just do if node(e) = node(v) + node(w) (ie not >)
just record duplicate there (i dont need to know the exact path)
if (dist[nextNode-'A']> dist[node-'A'] + weight){
if that was equal the path wouldnt be unique
11:55
no thats not right but it might not be on the path
7
Q: How to find whether the shortest path from s (any starting vertex) to v (any vertex) in the undirected graph is unique or not?

Sanket AchariGiven an undirected graph G = (V, E) with no negative weights. What is the complexity for checking uniqueness of shortest path for every vertex in the given graph?

12:17
struct comp {
    bool operator()(const string &n1, const string &n2) {
why do comparator overload the operator() in particular? what is the mechanism triggering this?
@Permian it's called a functor, but honestly if you're not holding state you could just use a lambda
 
3 hours later…
15:08
Is there any good materials or videos I can read about what "reflection" really is?
It seems that many things that dynamic languages can do while C++ can't do are related to someting called "reflection"
@Rick there's a bunch of static reflection tools for C++
and some standard proposals too
What I really want to know is, with "reflection", what those dynamic langages e.g. Python and JavaScript can do while C++ can't
it's like extended RTTI
@Rick Reflection is the ability for code to self-inspect. Examples of reflection that C++ can't do but many other languages do is getting a list of all values an enum defines or getting a list of a class type's members.
they can have the equivalent of a void* and just ask "what data and function members does the object have that this points to"
15:12
I know RTTI and I just reviewed it today.
and depending on the language you can even change the behavior at runtime, so add functions or change functions, etc.
@PeterT As for this, I can use a long list of dynamic_cast to do the same
but you can see where the cost for this would come in, if they don't have a JIT the additional indirection which you need for this to work isn't anywhere near free
well you still can't ask "list all this objects member functions with that start with 'get'" even with dynamic_cast
unless you want to manually list all types in a list and crank your template instantiation depth to near infinity to just brute-force search all names
But I think most people don't even ask for dynamic reflection for C++, just a more sane and unified way to do static reflection
I would like to give an example I met earlier.
A few days ago, I was asked to produce a json based on a class.
I don't remember very well. But it looks something like this
class A {
	vector<int> vec;
	string s;
};
And I want to serialize the class instance to json like how JavaScript does
Right, there's a bunch of tools that help with that. Cereal, MsgPack, boost serialization, etc.
But they mostly require some manual annotations
or external helper functions
15:21
And I was told that by default, C++ can't do that because there is no Reflection.
And I was like "WTF? Reflection? Again? So with reflection then C++ can do that?"
So how is reflection related to this?
right, because there's no "give me a list of all data-members and their type-info". So the naive approach doesn't work
Static reflection would allow you to generate a set of A's members.
Automatically.
Currently you have to provide the set yourself. If you add a member and forget to add it to the list, it won't be included automatically. You need to remember to update the list separately.
I know I can get some interesting informations by using dir(something) in Python.
@FrançoisAndrieux Sorry I don't know about static reflection...
@FrançoisAndrieux What is the set referred to?
@Rick Static reflection is what I'm trying to describe. The set refers to some structure that allows you to get a list of all the class' members.
Is it because e.g. Python adds many extra informations (so-called meta info?) to all objects, then makes it can do such things?
15:28
Yeah, it's both the extra info, and the way it does book-keeping of objects in general (object allocation, object model, etc.)
@Rick Yes. To achieve reflection you need to store some metadata about each class. C++ does not have a built-in mechanism to achieve this. You need to do record this information manually.
Ah Ok, at least I get the basic idea.
What about Google Protobuf? Are you familiar with it? I need to use it in the next few days. I watched a introduction video and got the basic idea about RPC yesterday. And I know Google Protobuf is just the protocol part of the whole big picture.
And I think Protobuf is mainly used for serializaiton. Is reflection needed?
Protobuf has tools to add that information too
Because you most of the time make proto file that contains the type information and you generate the c++ class code with a tool
But serialization is not necessary to require reflection right?
For example
You can serialize without reflection if you can otherwise provide information about the structure of the object to serialize.
Reflection lets you get that information automatically. If you don't have it you need another mechanism.
Some tools might automate generating that information by parsing the source code.
15:37
The client has a list of classes. Class A, Class B. The server , has the same list. Class A, Class B.

In my mind, what serialization does is that the client extracts e.g. Class A instance's data. put it into a bytestream. Add something in front of the bytestream's header, indicating it's Class A. For exmple, a bytestream like this `IamClassA,data1,data2,data3`. And once the server got this, it `new` a Class A and put the data back.
It is usually not enough to look at the memory representation of an object to serialize it. You usually need to know how to interpret it. For example if you have a string type with a pointer to an array the serializer needs to know that it is a pointer and that it needs to also serialize what it points to.
If you consider a std::vector<std::string> the serializer needs to know that some part of the vector is a pointer to an array of std::string and then recursively know that each of those elements also has a pointer.
If you consider an std::list the serializer needs to understand that it needs to follow the chain of pointers to serialize the whole linked list.
All of this layout information needs to be available to the serializer somehow.
You can't guess it from just the bits of the object being serialized.
Reflection is just the ability for the compiler to generate that information automatically.
But can't these type infomation be encoded in the stream?
@Rick They could, but they need to be given to the serializer in the first place.
Ok. Enough for today..
Let me find some eaiser things online
It is a good exercise to try implementing a generic serialization function.
15:43
Thank you both very much.
Hmmmmm
I would like to give a try once I really get the idea.
15:53
@Mgetz thanks that was i what was missing, interesting stuff, never dove deep before
 
2 hours later…
17:31
hey guys, does the following code creates another std::shared_ptr upon std::vector::push_back?
std::vector<std::shared_ptr<Foo>> foos;
auto foo = std::make_shared<Foo>();
foos.push_back(foo);
and so, should I do foos.push_back(std::move(foo)) instead?
If you don't need the foo anymore you should use std::move to avoid copying the shared_ptr.
Moved constructing a shared_ptr does not need to update the control block. Copying one does, which can have a performance impact in a threaded environment.
@Henri You are correct that this code sample would cause a copy of foo.
copy of foo or copy of std::shared_ptr?
foo is a std::shared_ptr<Foo>. foos.push_back(foo); copies the std::shared_ptr called foo.
18:01
oh, duh.
well, can someone also help me with code optimization? I'm implementing a QuadTree and here's the node insertion code: pastebin.com/CJdmm5bQ
I'm unsure whether it's optimized enough or not, in terms of copying/moving/references etc
18:35
Hello, I have what might be an easy question to answer: What does the term "straight-line program" mean? Is it just a single line of code or simply one not using recursion or loops?
I don't think it's a technical term. Could mean very little branching, could mean few seperate functions, could mean a lot of things.
oh, huh looks like there is a technical term like that en.wikipedia.org/wiki/Straight-line_program
Reading this, it doesn't make sense to me
Alright, how about this: Given a stack, is it physically possible to compare the top two elements of the stack with one line of code?
(Wait, this is group theory)
(Not talking about programming, but it seems to just be describing a series of elements of a group)
Except for preprocessor directives, an entire c++ program can sit on the same line. Asking if something can be achieved in one line of code is not very meaningful in C++.
Fair enough
So, if you have a class "stack" which contains an array. The "top" function returns a reference to the last element of the array and "pop" removes that element from the array. How would you compare the top two elements in one line of code?
It depends if you expose the implementation detail that your stack is backed by an array.
If you guarantee the elements are in a contiguous array then you can use pointer arithmetic to get the element under the top element.
But you usually don't want to expose implementation details, and you would probably not want to guarantee contiguity unless that is the goal of your container.
18:47
Well, the array is private. Surface level, you only have access to those functions already mentioned and a few others
The "one line of code" requirement is irrelevant. You just have to come up with any solution that works, and then remove redundant white space until it is all one line.
The storage used by a std::vector is private as well, but it still guaranties contiguous elements. So you know you can perform some pointer arithmetic on the elements.
If you do not have this guarantee then, from the interface your describe, you must remove the top element to inspect the next one.
So, perhaps I should say "only uses one semicolon?"
One solution may be to copy the top element, pop it, look at the next element and put the copy back on top.
You can still chain expressions using the comma operator.
You may be asking if you can do it in a single expression.
But you can then just take your solution, put it in a function and call that function to achieve that goal.
stack.pop()==stack.pop();
pop doesn't return the element, only removes it
18:52
well then
stack.top()==(stack.pop(), stack.top());
still not code you should ever practically write
That's kind of wild
just write it with normal code, no one appreciates having to read that stuff
This is for an assignment, so I'm trying to make sure I do the assignment correctly
It said to make a "straight-line program," though
I was trying to rule out the possibility of that meaning "do it in one line of code"
Ask your instructor what they mean.
yeah, I doubt that they meant one-line
18:57
Not the most accessible instructor. Last time I asked them for help, they directed me towards stackoverflow (which I was already familiar with, and its good advice, but still)
(Now I want to try and do this in one line anyways. Probably won't do this for the final result, but still)
How about
x=((x=stack.top()) <= (stack.pop(), stack.top())) ? stack.top() : x;
The goal is to store the larger of the two in x, in this situation
There is no merit in compacting code. It is probably not what you are supposed to do. If it is, then the question has no use in measuring or improving C++ proficiency.
Fair enough. I will write it out in a more readable fashion. Though, to sate my curiosity, do you know whether what I just wrote would function as intended?
I would say no because it isn't clear which side of the comparison will be evaluated first. Though there were changes in C++17 about order of evaluation that may have changed that.
Ah, I see the danger now. Even a person who knows C++ very well can't evaluate how it works at a glance
If the right side is evaluated first then x=stack.top() evaluates to the second element, same as the right side of the comparison.
19:08
Readability-ified:
x = S.top();
S.pop();
x = (x <= S.top()) ? S.top() : x;
That is way better and will work as expected. Consider using a different variable for the result of the last expression though.
Also consider using std::min(S.top(), x); instead, which self-documents the intent of the code.
It should be the largest of the two, so, the max, right?
Or rather std::max.
Also, the assignment clearly states to only use one variable and one comparison
So, gotta use the one x. That one is certain
I would be suspicious of the qualifications of whoever wrote your assignment.
19:17
Well, they worked at Sandia Labs, so they have experience, at the very least
Next one up is "Describe how to implement a stack using two queues."
If you have two queues, you can use one to reverse the other
If you reverse, pop, reverse a queue when you pop it, you effectively have a stack. I don't see how else you can do it.
There might be a smarter way, but I can't think of it. This would be super slow though. Popping would have linear complexity.
Yeah, that's what I was thinking. Use on queue to store the variables and then the other to cycle through to the end of the queue whenever a pop is called.
20:30
Not sure if this is allowed but in my view: "a question's a question".
So here's a link to a question 've been attempted to solve for a few days straight now: https://stackoverflow.com/questions/64598755/c-template-class-type-alias-failing-substitution-in-member-declaration
(if posting the question as a link is against rules, feel free to warn me & remove it)
Some context to the question: It's on how type aliases of template classes are interpreted in member function definitions
21:30
Hello guys I just found out about this chat thing
👋🏾
 
1 hour later…
22:50
@Krapnix hi, welcome

« first day (1571 days earlier)      last day (1506 days later) »