« first day (1425 days earlier)      last day (1652 days later) » 

03:20
A base class `A` and derived class `B` are implemented as such:

```
class A; class B;

class A { private: int value; public:
constexpr inline A(int const value) : value{value} {}
constexpr inline A(A const& a) : value{a.value} {}
constexpr inline A(B const&);
};

class B : public A {
private: enum {off, on} whatever;
public: constexpr inline B(int const value) : A(value), whatever{on} {}
};
```

with class `A` specifying a multiple constructors including one that accepts a class `B` object.

=============================
The A::A(B const&) member function is meant to call A::A(A const&), as well
04:05
you're casing a value in the first case, doesn't that mean that a new value needs to be constructed?
So the cast itself "(A const) b " is calling the constructor, right?
Also, shouldn't you want to use a delegating constructor call instead of creating a new temporary instance of A
@PeterT Yes, the expression (A const) b is calling the constructor repeatedly but I actually want to treat the object b like an A object instead.
Sorry about that:

`A::A(B const& b) : A((A const) b) {}` or
`A::A(B const& b) : A(*((A const*) &b)) {}`

This will do, right?
That does use a delegating constructor, but how does that change the cast recursively calling the constructor?
do you mean to assume that B you pass in, is just an A? <-- nvm I read the inheritance wrong, it always is
then something like "static_cast<A& const>(b)" might do.
(or if you insist on C casts "(A& const)b" )
But why do you even need the overload for B?
If you don't have the overload for B then the other one will be called automatically
04:26
@PeterT Yea, actually.
@PeterT The class A actually contains a raw pointer member that I'd rather have copied element-by-element (i.e.: memcpy(x, y, n)) and not just by value (i.e.: x = y)
When the overload is not specified, the raw pointer member that A has only copies the address instead of the contents of the address
After a bit of reading, I think it goes like this:

`(A const) b` is more-or-less just `A{b}` (not my intent 👎)
`(A const&) b` casts `B` to its `A` parts only (treating `B` like an `A` 👍)
`*((A const*) &b)` evaluates the `A` part of `B` thru "pointer magic" (treating `B` like an `A` 👍)
 
5 hours later…
09:38
hello
um I am looking for somthing of this type in c++
suppose i have a vector of integers and value of each element of vector serves as weight and I want to get the random index weighted by value
what I mean is that suppose my vector is [1, 2, 4] then roughly I should get index 0 half the times as index 1 and index 1 half the times as index 2
so uniform probability distribution of indices is [1/7, 2/7, 4/7] so how can I achieve this in c++
09:58
Hi, in c++11, we know this is list initialisation: SA a{}; is this the same kind of initialisation as it: SA a = {};
@jeea I don't know, maybe just generate a uniform number between 1 and 7 and then just sum up the numbers in the vector until you reach that number
SA a{}; is not list-initialization
it's actually value initialization
SA a{1,2,3}; is list-initialization though and
SA a = {1,2,3} is copy-list-initialization
if provided no arguments, is Sa a = {}; value initialization or copy-list-initialization?
10:14
I don't even know, my guess is value initialization and if Sa is an aggregate then aggregate initialization
ok thanks a lot!
 
3 hours later…
13:40
struct Comp {
        bool operator()(const pair<int, string>& lhs, const pair<int, string>& rhs) const {
            if (lhs.first != rhs.first)
                return lhs.first < rhs.first;
            return lhs.second > rhs.second;
        }
    };
why is operator() used here?
im not overloading a symbol
nwp
nwp
It's a comparison operator. std::sort for example wants to have some f where f(element1, element2); is a valid expression and returns if element1 is less than element2. To make that legal syntax your predicate must have the call operator.
but theres no sort to overload?
i cant see sort anywhere
nwp
nwp
Then maybe it's used in a std::map.
nwp
nwp
So it's used in a std::priority_queue. That one needs to keep objects in sorted order. It uses an instance of Comp to decide that order.
13:48
but comp is a defined thing, its not overloading another thing
nwp
nwp
It's a template parameter. Somewhere inside the implementation of std::priority_queue it uses the Compare parameter to decide the order of objects.
hmmm.. my first thought is that is called Comp not Compare
nwp
nwp
Well, they are the same thing in this case.
Compare being the parameter and Comp the argument.
tricky stuff
is this called a function object?
nwp
nwp
Yes.
It behaves like a function.
14:00
interesting. never seen it before outside of c++ primer
why can i just supply a normal function?
nwp
nwp
You can.
interesting
nwp
nwp
Well, maybe not.
why do this then?
nwp
nwp
I have to check.
The issue is that the interface requires you to pass a type.
A function is not a type. You can get the type of the function, but then you don't know which function you mean anymore.
14:04
i see no reason why it cant be a vanilal function?
nwp
nwp
Normally you pass in an object like with std::sort and there you can pass a function just fine. But std::priority_queue is weird and doesn't let you pass an object, only a type. The struct is a way to put a function in a type. I don't think you can do it directly with a function.
@nwp genius!
nwp
nwp
C++20 lets you do this. Before C++20 they didn't allow lambdas in unevaluated contexts and they weren't default-constructible. I think.
14:31
@PeterT Nice idea, so maybe i binary search, or use upper_bound on prefix_sum vector of weights?
maybe lower bound
yeah, that's pretty much what comes to mind without thinking much about it. There's probably something better, but nothing I can think of
Maybe this O(logN) should pass considering random is giving answer in constant time
15:16
Any of you guys good at regexes?
nwp
nwp
Bad question. Ask your real question.
Oh Sorry, silly me
See, its like this,
I want to replace all the spaces written in my lists [ 1 2 3 3 2 1 4 5 6 5 4 7 8 9 8 7 5 2 3 6 12 14 25 21 12 12 12 14 24 42 52 52 25 46 97 85 85 58 64 46 46 41 37 79 19 91 73 82 64 91 73 92 72 18 38 55 64 46 97 79 ] with commas
so that it becomes
[ 1,2,...,79 ]
Notice that the first and the last spaces have been kept intact
and ... is because I'm a lazy types
*typer
nwp
nwp
Hmm. Normally I'd recommend regex101.com, but that doesn't seem to be very useful in the replacement case.
Yeah that's what
nwp
nwp
\d+ \d+ is what I'd start with. And then replace the space with a comma which actually falls outside of regex.
15:22
Umm.. I didn't quite get you. Am a complete noob in the area
I don't think you really need a machine gun to kill mosquito, regex for that! Look uo strip, split
nwp
nwp
regex101 is a really useful website when you want to make a regex. It explains a lot and it's very useful to put in test cases and check what you want to match.
Uh no, Its not to be written as a python code, its the find replace type regex in gedit
nwp
nwp
\d matches a digit, + means 1 or more, the space is a space. So it looks for a number followed by a space followed by a number, therefore excluding the first and the last space.
I'm unsure how to express the replacing thing though. Can you just do \d+,\d+ and gedit knows what you want?
But the problem is not looking for the regex. Its replacing a part of it
nwp
nwp
15:24
Rip.
Hahaha
Cause, [1-9] finds it nicely
can vim, emacs pros do this in their editor in a blink of an eye?!
Yeah they can
That's why they are called pros
nwp
nwp
They can if you know what to type in.
I am a pro when it comes to vim, but still haven't quite mastered regexes
nwp
nwp
15:27
I don't know how [1-9] gets around the leading and trailing space problem.
No look carefully,
its [1-9]<space>
That's essentially what I want
A space after the last digit
So SO I guess?
Can't even think of the DV count that'll receive
nwp
nwp
That matches 79 ] which is incorrect.
But I guess replacing the last comma with a space isn't that difficult.
Yeah right
how about ([0-9]+)[\s]+([0-9])
replaced with $1,$2

just might need to run it twice if it doesn't have overlapping match support
Can you explain $1 and $2 please?
15:31
that's just matching groups for replacement
the parantheses
But wont that replace numbers too?
it will replace it with what matched
so with the original text
Hmm... need to try
here you can see it after one pass: regex101.com/r/Qo8m2A/1
Whoa
Nice
Thanks @PeterT :)
Hmm... can we improvise it to one pass?
@PeterT, I still have no clue as to how it works. Will you please explain
15:43
the page has good explanations on the right
Oh! okay thanks :)
Will take a look
Uh I was trying regexes there (Awesome site BTW), And I came up with find:([1-9])( ) replaced with:$1,
How do I exclude that last bracket in this case?
15:59
that's why I matched the two numbers, if you want to exclude match you need to look forward at least as far as after the space
and then your match includes the start of the next number, that's why I suggested just doing it in two passes
Find: ([1-9]+)( )
Replace with: $1,

Sorry if you find me annoying, but I really want to learn this concept
what ever is in the first set of () is $1
$0 is the full match $1 the first () , $2 the second ()
it's basically returning the same thing as this: en.cppreference.com/w/cpp/regex/match_results/operator_at
Ah...
So one can never match just the initial occurences which do not include ]
Because the moment you look further than the space, that too immediately gets included in the final search pattern
There might be some weird backtracking matching stuff, but I never messed with batracking
because honestly if you enable backtracking then why would you still call it regex?
that's at least context free iirc
Oh god, so there's another concept too?
16:16
no I just meant formally, like chomsky language hierachy wise. There's no widely used domain specific language for context free grammars like perl regular expressions
@PeterT, Hmm... Well, what's wrong with \[ ([1-9]+ ){1,}\]?
Sorry, typo
\[ ([1-9]+ ){1,} \]
16:59
nothing wrong with that, I just don't know how you would replace each match of the capturing group
At some point, you can also just over-use regex. Simply stripping the brackets conventionally and then matching the rest with a regex seems much more simple to me
17:56
Which storage class should I use if I want to use a variable from various files and modify it in different functions across these files?
If I uncomment get(), I get "undefined reference to f" error
So extern seems to be pretty useless.
18:21
well it's useless if you don't let it be not extern in exactly one translation unit
A.h
extern int f;
A.cpp
int f = 0;
B.cpp
#include "A.h"
0
Q: Using a variable across multiple files without declaring it with extern

ArcherI have an array of typedef struct Books Book in a file1.h declared as Book books[20]. Now, I initialise the array in file1.c and then use it in file2.c (that includes file1.h), without defining it. The program works fine. But I don't understand why. Since I have not used extern with books, shoul...

I posted my question on main ^
gotta close that as duplicate stackoverflow.com/questions/16390023/… :P
hm, you have "Book books[20];" in both files and don't get a linker error?
@PeterT I don't.
just paste the whole code in the question, the description is confusing to me
@PeterT It's too big.
I am posting snippets of it.
@PeterT I have added some code, is it understandable now?
18:33
@Archer do you also include "file1.h" in file1.c ?
yes
and you're building both files as seperate translation units and not in a unity build?
oh

> Non-const global variables and free functions by default have external linkage;
@PeterT Not sure I understand translation units. I am compiling them together to get an executable.
@PeterT So books is of type extern right?
Also if the question is better now, please upvote.
I didn't vote on it
struct X { const int n; };
union U { X x; float f; };
void tong() {
U u = {{ 1 }};
u.f = 5.f; // OK, creates new subobject of u (12.3)
X *p = new (&u.x) X {2}; // OK, creates new subobject of u
assert(p->n == 2); // OK
assert(*std::launder(&u.x.n) == 2); // OK
assert(u.x.n == 2); // undefined behavior, u.x does not name new subobject
}
Hi, why is the last statement undefined?
The explanation to it is not really clear.
19:04
MAX_ITEMS is #defined constant.
defined to be what?
And VS code gives this error: "expected an expression"
@PeterT 20
give me the exact line
> #define MAX_ITEMS 20
seems like that should work, I thought you had a semicolon at the end
19:06
Doesn't work, error goes away if I replace it with 20.
are you sure the define is included in that file?
yes, it is included in the header of that file.
i.e. in file1.h
and you didn't accidentally #ifndef it out by accident? I can really just guess here, since nothing you posted should cause the error
@PeterT I did.
So #defines should be above ifndef?
no
I just meant if you accidentally used the same ifndef symbol twice
19:11
here's the full header^
and you don't define USERINTERFACE_H_ in any other header file?
@PeterT No
I don't see the error there, is that the only error that you got
@PeterT No, wherever I have used MAX_ITEMS I have got the same error
19:25
I Understand that when you do a git commit, it stores a tree along with the commit author and message. But is this tree a copy of the entire folder structure?

« first day (1425 days earlier)      last day (1652 days later) »