« first day (1356 days earlier)      last day (1496 days later) » 

3:01 PM
Any advice on how to restart two threads instead of redclaring them each time in side a loop? Implementing the producer-consumer pattern seems excessive . stackoverflow.com/questions/60896525/…
 
well if it's about having two threads wait at a specific point you can use the new barrier stuff. But I don't think doing it by hand with a mutex, a condition variable and a couple variables to do the input/output is all that bad
but if you want the low-efford solution just do what one of the dudes in that thread suggested and use std::asnyc
 
3:35 PM
How would using async be better?
 
@northerner the system wouldn't be creating and destroying threads on each loop iteration
if it has a sensible implementation
 
 
2 hours later…
5:39 PM
hello!
is there any reason why you would like to throw an exception like throw new MyClass{} instead of just throw by value ?
 
None that I can think of, the only times I've not thrown exceptions by value was when rethrowing stuff with std::exception_ptr in threads/coroutines
 
and there is another question i have : if I have class E{} and inside void f() {throw new E} and my catch contains elipsis operator like this catch(...) why i get the following error terminate called after throwing an instance of 'E*'. Shouldn't elipsis operator catch ALL errors?
 
yeah, it should. Are you sure it was thrown in the try body?
 
yes
i can share you the code
class E {};

class X
{
static int c;
public:
X()
{
if(c++ > 2)
{
throw new E;
}
}

~X()
{
if(c++ > 2)
{
throw new E;
}
}
};

int X::c = 0;

void f(int i)
{
X a,b;
cout << i;

}

int main(void)
{
try
{
f(0);
}
catch(...)
{
cout << 1;
}
return 0;
}
@PeterT i dont know how to format it
 
oh you threw in a destructor
that's no bueno
 
5:53 PM
hmmm hard to understand:))
 
if you throw one exception then you need to destroy everything you created in that scope, right
so if you throw while trying to cleanup from one throw then you "double throw"
that calls std::terminate immediately
 
if I throw one exception, the promises tells me that the program will look as there nothing happened
and the objects are destroyed
ok but in my example i didn' tried to cleanup from one throw
oh... i see what you are telling me
 
the compiler was cleaning up, not you manually
 
like both a and b are throwing. There would be no problem if just one of them was throwing an error, right ?
and if both of them are throwing, one of the erros escapes as is written in the article
 
well, it's really best if you just never throw from destructors
 
5:58 PM
ok got it
thank you
@PeterT
cpa example test....
 
I'm not sure I understand, are they saying "print 01" is correct?
 
oh, because of the counter
 
what counter
 
X() X() ~X() don't throw and then the last ~X() throws
so in the first f() call only one esception is thrown
I mean "c"
 
6:14 PM
no
you have ` obj constructed => c=1 , second constructed c=2, 1st destructed c=3(error thrown) and then 2nd destructed c=4 (error thrown again)
 
no. x++ first returns the value then increases
so if you have int x=0; then do std::cout << x++; prints 0
 
but im like literary trying to run this code in my compiler
and it doesnt want to
it prints me 0terminate called after throwing an instance of 'E*'
 
really? weird maybe a typo when transcribing or did you copy paste it?
 
i copy paste it
 
oh, that's because of new C++ rules
destructors are by default noexcept
you can manally make them noexcept(false)
then you get the old version output
 
6:21 PM
thank you
which is the difference between catching void* or ... elipsis ?
 
void* would only catch exceptions thrown by pointer, right?
 
i dont realy understand what void* is
i cant understand a pointer to void . Or i didnt find a good example for understanding
 
it's not really useful to think of it as a "pointer to void" it's just "any kind of pointer" they just reused the "void" type there
you can never dereference a void* because it's not a specific kind of pointer, you always need to convert it to something else
 
a static cast would work ?
or what conversion you are talking about ?
 
6:37 PM
yeah, static cast
in C++, in C it was an implicit conversion
 
it is possible to throw an exception by reference?
class x{} and then` throw &x` somewhere inside code?
is any difference between this and throw x?
 
throw &x would throw by pointer
throw is like return, whether it's by reference or value is not determined there
 
is not determined there? what do you want to say
 
if you want to return by reference or by value you still always write "return x;"
since the exception handler will always need to store the exception you can just assume it'll always be practically by value
 
uf i dont get it
look I am following an example
E e;

switch(i)
{
case 0 : throw e;
case 1 : throw &e;
}
try
{
    f(1);
}
catch(void *)
{
    cout << 2;
}
catch(E*)
{
    cout << 1;
}
f(1) contains that switch
 
6:52 PM
did you mean fallthrough?
or did you forget the "break;"
 
well the parameter for switch is 1 this is the reason it throws the second throw
it doesnt matter if i forget or not the brake statement
 
true
 
but it enters the catch void*
and &e you just said is similar to return by alue
 
yeah, it's "first that matches" I think, not "best match"
no, I said &e is returning by pointer
 
because a reference is a pointer
 
6:55 PM
what? I think you're confusing "Type &value" and "&value"
&value means "take the pointer of the value"
 
@PeterT take the addres of the spot where is stored value
 
right
 
ok, and type &value?
 
that declares a variable called "value" of type "Type&"
I know it's stupid, but that's just what they made it mean
 
ok and type type& why is considered a pointer?
 
7:00 PM
what? type type&?
Type& is considered a reference, it's different from a pointer conceptually. But for the compiler they're mostly the same
 
ok and returning to the example i provided, there is happeining an automatic conversion ?
by throw &ref i will get an address?
 
yes you'll get a pointer
 
oh stupid me
 
if you turn on warnings the compiler will tell you "exception of type 'E*' will be caught by earlier handler for 'void*' "
 
thanks!
 
7:41 PM
more noob questions:
let's say i have an array of pointers to some object
and i say:
someObject* p = arrayOfPointers[5];
does "p" now point to the same address that arrayOfPointers[5] points to?
or does "p" point to a different address that points to a copy of what arrayOfPointers[5] points to?
... whoa, wut
wouldn't it point to arrayOfPointers[0] if i said
 
sory i misunderstood
 
someObject* p = &arrayOfPointers
ah. no worries
 
p points to the same addres that array of pointers[5] points to
 
ah, cool. that's what i thought. thanks :)
 
but i'm not sure it is the right answer
int main()
{
int a=2, b=3, c=4;

int* p[3] = {&a, &b, &c};

int *ptr_to_int = p[3];

cout << ptr_to_int;

return 0;
}

For example i would expect dereferencing ptr_to_int to have the same addres with c's address. but instead i got an error...
@AmagicalFishy
 
7:55 PM
i think you're getting an error because of the line
int *ptr_to_int = p[3];
p[3] would be undefined behavior, since there's only p[0], p[1], and p[2]
@CătălinaSîrbu
 
yes
but their addresses are the same
thanks for seeing the mistake
 
 
3 hours later…
10:48 PM
Hello world
 
 
1 hour later…
11:51 PM
#include <iostream>
using namespace std;
class X {
public:
    X(void) { cout << 0; }
    ~X(void) { cout << 2; }
};

int main(void) {
    try {
        X *x = new X();
    throw true;
        delete x;
    } catch(bool s) {
        cout << s;
    }
    return 0;
}
what happends with this code? why isn't the X's destructor never called?
 

« first day (1356 days earlier)      last day (1496 days later) »