« first day (1617 days earlier)      last day (3330 days later) » 

11:00 PM
@райтфолд b < a ? b : a
 
user1804599
Sort, not min.
 
right
 
user1804599
std::sort(fieldTypes.begin(), fieldTypes.end(), [] (auto a, auto b) {
    return b->size() < a->size();
});
 
user1804599
I want the field type with the largest size to be the first element in the vector.
 
emplace forwards the provided arguments to the appropriate constructor
wait I'm tripping, this got answered
 
11:01 PM
shouldn't that just be b < a ? b : a
 
@райтфолд I think that's right.
I once implemented std::set reverse comparator it as !(a < b) and this actually caused crashes.
 
Good job.
:D
 
Because that does not give a strict weak ordering.
IIRC return b < a is what you need.
 
Are pass by values always rvalues?
 
11:05 PM
 
I see, thanks. And mine is the only reference to Weisert.
I'll consider preparing a recommendation for it, OOCCJ.
Over and out.
 
Okay, but move semantics only applies to "rvalues" or "xvalues" that are limited in terms of scope. When you want to the value of that rvalue/xvalue, you swap your copy target and the rvalue/xvalue, then let the automatic cleanup crew of the scope destroy your "overwritten" original
 
@Cinch I suggest you use static asserts or enable_if on std::is_rvalue_reference. I found that type traits a useful educative tool during experimentation.
 
In other words, move a temporary instead of copy.
 
user1804599
For now I'll just make it box all objects.
 
11:08 PM
@Cinch Copy/move and swap is hardly the only implementation available.
 
user1804599
Also, why can you not return a temporary by reference to const?
 
Something I just saw: "So I'm having a bit of SegFaults...", it really made my day....
@Mikhail entanglement is pretty damn real
 
@vsoftco Basis of quantum computing, anybody?
 
here
 
@StackedCrooked Right, because that is satisfied if a == b, which gives the problem that comp(a, b) == comp(b, a), which is super bad.
 
11:09 PM
:)
 
@Puppy i still can't wrap my head around move semantics
 
@Cinch rvalues trigger the move constructor. lvalues trigger the copy constructor. std::move(lvalue) returns an rvalue which triggers the move constructor.
 
@StackedCrooked so uh... what ARE rvalues?
I still don't get it.
 
@Cinch The simplest way of thinking about it is this: moves are just copies, but nobody gives a shit about the state of the copied-from object.
 
"5" is an rvalue
 
11:09 PM
no it isn't
5 is an rvalue, "5" is most definitely an lvalue.
 
No.
int i = 5; //5 here is an rvalue
 
yes, that's 5 and not "5".
 
rvalue is like an poor person which does not have a legal domicile.
 
const char* str = "hello"; //hello is an rvalue
 
no it isn't.
it's an lvalue.
 
11:10 PM
but isn't "hello" a constant given to the compiler?
 
yes.
 
then how is that not an rvalue?
 
string literals are special
 
one simple mnemonic: can you send it a postcard? (take it's address?) if not, then you have a prvalue
 
because it's really an array.
and native arrays have some seriously fucked up rules that you don't want to know about.
 
11:11 PM
@Puppy Good explanation.
 
and lvalueness and rvalueness is one of the ways in which they're especially fucked up.
 
Okay so arrays are always lvalues by nature?
 
the simplest thing to do is just pretend that native arrays don't exist because they're so fucking annoying that everybody uses std::array instead and they have a trillion special cases.
 
Dunno about arrays.
 
Okay.
But why would I want to use std::move?
 
11:12 PM
ah
 
@Puppy you really hate C :)
 
Why would I convert an l -> rvalue?
 
A pointer to a string literal will never dangle.
 
@Cinch because you want to move, duh
 
const char* foo() { return "abc"; } // OK
 
11:13 PM
@vsoftco The fun part is that the non-quantum style entanglement is also real! Indeed, the quantum style entanglement has yet to be realized experimentally.
 
@Cinch Because you explicitly don't want the source object to be preserved.
which has a number of uses
 
@Puppy So basically, cut and paste vs. copy and paste.
 
yes, roughly speaking.
 
Is there a defined standard on what happens to the original?
 
@Mikhail no it is not, it was already tested experimentally by Allan Tapp, who experimentally tested "Bell inequalities", which show that quantum correlations are "stronger" than any classical ones
 
11:14 PM
not really.
 
But don't they always get destructed?
Or something?
 
yes.
but
well, it's complicated, but a moved-from object is a bit of a strange state that every type defines for itself what's valid to use.
but destructor should always be valid since the language and every C++ library unconditionally calls it all the time.
 
I don't even.
 
any other function though and all bets are off unless the specification for that component explicitly states otherwise.
 
@Cinch don't fuck the moved-from object up so much that you can't destroy it anymore :p
 
11:15 PM
@Mikhail entanglement is just "correlations", the fact is that in Quantum Mechanics you can have "stronger" correlations than any classical ones that you can obtain vs flipping coins, since in Q.M. you can have what it's called "incompatible bases", e.g. systems in which A and B is DIFFERENT from B and A
 
however, it's generally accepted that moves should place the source object in some valid state, even if that state is not defined, so functions without preconditions like assignments should still be legal.
 
Try implementing your own unique_ptr with move semantics. It will quickly become clear.
 
@StackedCrooked I don't even understand that yet.
Why is a unique_ptr necessary?
 
ah
 
@Mikhail but this discussion should probably belong to a Physics room
 
11:16 PM
it's about the nature of cut and paste vs copy and paste.
 
I get the shared_ptr because things often need to have a reference to a common object.
 
imagine that you have a class which deletes some resource in the destructor.
doesn't matter what the class is or what the resource is.
 
@Puppy i.e. std::vector
sorry, std::list
 
@Cinch If you like.
doesn't matter- the same principle applies to pretty much all of them.
now it's obvious that if you have a std::vector, and then you produce another std::vector with the same content, you can't have them referencing the same resource.
because then you would destroy it twice in their respective destructors.
which is Super Bad™.
which is why the copy constructor effectively creates a new resource with the same value as the old one.
 
user1804599
@Puppy I don't see the use of that for anything other than assignment.
 
11:18 PM
so every instance of std::vector owns exactly one copy of that resource.
 
Is there a way to do in-place/no copy construction creation of a vector element without requiring a specialized constructor?
 
but now imagine
that you have some guarantee (in this case rvalueness) that nobody gives a shit about the previous resource anymore.
 
@Mikhail I meant "Alain Aspect" not "Alain Tapp"
 
user1804599
My I/O library throws bad_fd when calling native_handle on a moved-from unique_fd.
 
@Pris "Specialized constructor"?
 
11:19 PM
so you don't copy the resource at all.
 
@Pris emplacement should work with any constructor (as long as it's public)
 
all you have to do is tell the other vector not to destroy that resource in it's destructor so that it's safe for you to destruct it.
then you can reference the resource right away.
 
@Puppy But, er, how do you do that?
 
@StackedCrooked I meant on a POD, should have specified that
 
@Cinch Typically, you just set the vector to empty, which means setting the internal pointers to nullptr.
which is also how unique_ptr is required to work by Standard I believe.
 
11:21 PM
@Puppy Wait, but what happens when it goes through the destructor?
 
POD move is identical to POD copy.
 
@Cinch Nothing.
 
Isn't it bad to destruct a nullptr?
 
nope.
delete (T*)nullptr; is perfectly safe for any T.
and even if it wasn't it's trivial to just do like if (ptr != nullptr) delete ptr;
so in this case, unique_ptr, we say that for each resource, it is owned by exactly one unique_ptr.
 
@Pris You can't gain performance benefits with move or forward when dealing with PODs. If you want to avoid uneeded copies then you can just use the old-style pass by const-ref (const T&)
 
11:22 PM
and obviously if you tried to copy a unique_ptr, you'd have two of them, which means double delete.
 
@vsoftco There is some debate about this, a professor whom I took a few classes would proudly assert that it still remains open: journals.aps.org/prl/abstract/10.1103/PhysRevLett.111.130406
 
so you can only move a unique_ptr by stating that you don't need the old object to still own that resource.
 
@Puppy So... you can't copy it?
 
@Cinch Right.
because then you would have two unique_ptr that point to the same resource, which leads to double destruction.
 
What if I were to create three pointers: shared_ptr, unique_ptr, and a regular T*?
 
11:23 PM
don't.
 
The unique_ptr wouldn't stop any operators with other pointers, correct?
 
ah, well
 
@StackedCrooked I can create a POD directly with an initializer. If I wanted to do that but in a vector without the extra copy, I can use emplace_back but then give the POD a specific constructor (so its not really a POD anymore I guess). Or maybe I can use placement new... but then thats not a vector, and pretty nasty.
 
depends on which pointer you're talking about.
 
It cannot reference count the entire program because C++ doesn't run in a virtual machine?
 
11:24 PM
T* is perfectly safe as it does not do any destructing or ownership operations.
shared_ptr not safe as it is an owning pointer.
 
Mmm I see.
 
every object in a correct C++ program has exactly one unambiguous owner.
no more, no less, at all times.
 
@Puppy Unless you forget to destruct it.
 
ownership rules guarantee you won't forget. Because the compiler never forgets to emit destructor calls.
 
@Mikhail don't go into the "non-locality" and "non-realism" business. I am also a graduate from Robert Griffiths (quantum.phys.cmu.edu/CMU/index.html), who managed to convince me the whole issue is bullshit. You can read his book online for free, and see why all these so called "paradoxes" appear, and I'm pretty sure you'll be convinced.
 
user1804599
11:25 PM
void set(Value* container, Value* member) {
    std::memcpy((char*)container + offset, &member, sizeof(Value*));
}
 
user1804599
Absolutely wonderful.
 
@райтфолд This is for a new language or machine, I presume?
@Puppy So can shared and unique collide?
 
Ah it took me this long.
 
i.e. if I tried to copy a shared_ptr into a unique?
 
But I finally understand why axioms are a big deal in Concepts.
 
11:26 PM
can't be done.
the unique_ptr won't accept a shared_ptr in the constructor.
the other way around is fine as long as the unique_ptr is an rvalue, of course.
 
std::unique_ptr<T> to std::shared_ptr<T> or other way around?
 
but if you were to say, manually get the pointer out and construct a unique_ptr with it
then that would be extremely bad as you have just violated the one owner rule.
 
I meant PhD graduate, and this dude is one of the scientists of our century who worked with Richard Feynman, so I have a huge respect for what he's done.
 
which directly means imminent double destructions and such fun things.
 
@Mikhail but I will now stop this discussion, as I came here to relax from my day-to-day fuckin' research job
 
11:28 PM
You could do shared -> unique by doing shared.unique() ? shared.get() : nullptr probably
 
wanna see some "classical" machines kicking in
 
@Rapptz Race condition- and you'd have to release the shared afterwards.
 
1
Q: Int values changing drastically and segmentation faults

IocustI've been trying to learn C++ lately, and I'm working on making a linked list of intervals that have no intersection, but i've been getting some really weird errors. My integers (obtained from get_bi or get_bs) give some pretty ridiculous values after I run the reuinion function the second time. ...

 
once you put it in shared_ptr you can never go back
 
but in theory, you could indeed do this, and you could also do a somewhat similar jobby if you said that the unique_ptr owns one reference to the shared object.
 
11:29 PM
This is due to rvalues and lvalue problems, right?
 
He assigns a temporary value from the constructor to an object: that is invalid.
 
The stack variable disappears and he tries to access it
 
no
it's only unsafe to reference a stack variable, which he does not do.
copying or moving from a stack variable or temporary occurs all the time and is completely safe.
 
11:30 PM
But isn't Foo f = Foo(args) causing the problem here?
 
no.
at least, not in as of itself.
 
Oh wait.
No the problem is that it's inside a function.
 
@райтфолд Your message id is also cool: 22226699. But mine is still cooler. Because I'm cooler than you.
 
it certainly could be if he incorrectly implemented one of his constructors.
 
did you read the answer dude
come on
 
11:31 PM
but that would be another matter.
 
He uses a temporary object to hold the new object and then sets the member variable pointer to point to the stack variable.
 
@Puppy not if you move outside the stack, right? Can you transfer ownership via std::move(stack_local_var)?
 
Once the function finishes, the object point is pointing to an invalid object that has gone out of scope.
 
@vsoftco Of course.
@Cinch No, once the scope ends.
 
That's correct, right?
@Puppy But the scope is the function's
 
11:32 PM
@Puppy of course yes?
 
the function finishes, the stack collapses, and the object "with it"
 
user1804599
@StackedCrooked That's cool.
 
@Cinch If it's an argument or on the top-level scope, then yes it would be, but I don't see that occurring in any of the questioner's functions.
 
Yeah.
 
Thus, SEGFAULT because he is accessing a variable that has gone out of scope?
@Puppy void reunion(args)
isn't that right?
 
11:33 PM
@Puppy then can I just return std::move(my_favourite_stack_var_in_my_function)?
 
@vsoftco And this sets the precedence for a factory function.
 
@vsoftco std::move is possibly unnecessary here.
 
better don't so that you can get copy ellision
 
@Cinch Yes, but he never takes the address of any of the arguments.
@vsoftco If you're returning by value, it's perfectly safe although not necessarily optimal.
 
@Cinch yeah, I know this, was just curious whether moving a stack local object is saft
 
11:34 PM
@Puppy he takes the address of the instantiated variable new_interval
 
@Cinch Which is not an argument, it's in a distinct local scope which is terminated as soon as he takes the address, or after a few statements, depending on which one you're referring to.
 
@Puppy I thought it's terminated once the function ends?
 
@Puppy damn, I gotta go back and see how rvalue refs work
 
@Cinch No.
 
But don't local variables inside of a function remain until it is finished?
 
11:35 PM
no.
 
What?
 
they only remain inside their scope until it is finished.
if you are inside another scope, like an if scope there, then it's the lifetime of that scope.
 
@Puppy if you return by value you shouldn't std::move, as you are (possible) disabling RVO
 
if you're a temporary the scope is even more limited.
 
@Puppy So it lives inside the {}
 
11:36 PM
@Cinch There are other potential ways to denote the scopes, but yes.
 
@vsoftco I'm sure he knows that :)
 
@Puppy I am just asking type&& f() { return std::move(local_var)}
@Puppy is this OK?
 
Well okay, but regardless he's accessing an object out of scope
 
@vsoftco You didn't mention that it was type&& instead of type. That's not OK at all.
 
Which means -> segfault.
 
11:37 PM
@Cinch Right, but that has nothing to do with the lvalueness or rvalueness of it. The same rules apply.
 
@Puppy thanks! I am happy now! :)
 
@Cinch If you're lucky.
 
@Puppy So it was a scope problem, not rvalue/lvalue
 
@vsoftco Returning T&& is almost never ok. (TBH I don't know in which cases it's OK, but I heard there are rare cases.)
 
Okay.
 
11:38 PM
Like std::move for example.
 
@StackedCrooked That would be returning an rvalue ref in general. Returning a ref to a local is never OK.
 
@StackedCrooked you may want to return a rvalue ref. in iterator cases, so you can further move from them
 
@vsoftco Banned by Standard.
all iterators except input must produce lvalues.
 
Wait, I thought we could use rvalues to create a factory function?
 
@Puppy hmmm, one more thing learned today
 
11:39 PM
like Foo* f = function_that_returns_new_foo();
 
no, don't do that...
 
Bad design, yes, but it is possible?
 
sure.
 
Isn't that what move can do?
 
completely unrelated.
 
11:39 PM
Move can take a temporary and then make it so that it becomes passable?
 
user1804599
@Puppy Bullshit.
 
Confusion ITT.
 
damn, I fell like I need 10000 years to learn all the subtleties of C++, I started just 1 year ago :(
 
Or it takes it out of the stack?
 
user1804599
It's perfectly fine if the local variable is static. :P
 
11:40 PM
@Cinch No.
 
@vsoftco I'm over here at 6 months. Hi!
 
the function cannot be implemented so that the return value points at a local.
the usual implementation is to dynamically or statically allocate the return value instead.
 
Well, can't I do this:
int* return_new_int()
{
int result = new int(0);
return std::move(result);
}
 
sure you can.
but it's the pointer that's being moved.
not the object.
also you're missing a *.
 
can't I move the object?
 
11:41 PM
std::unique_ptr<Obj> return_new_obj()
{
 
it would need to be int* result = new int(0);.
 
Just to be clear on one thing: you can't move memory. The contents of memory location A can not be moved to memory location B. It can only be copied.(However, you can emulate the move by erasing the contents of A after having copied it to B.)
 
fail
 
@Cinch In this case, the object is the pointer.
you can move from the actual int itself if you want to but that has nothing to do with the code you just posted.
 
No, sorry, if it were to return an int&&
 
11:42 PM
@Cinch well, I knew C++ quite well before :) E.g., most common C++98 stuff, but the new C++11 stuff really made me think about programming much more seriously. However, I find that the almost unbounded number of particular cases make C++ a bit less elegant.
 
you would have to move from *result.
which you could do.
but it would be a stupendously bad idea.
 
Let me rewrite
 
I have a quick question about things
 
strictly, there's nothing illegal or undefined about return *new T();, even if the return type is T&&.
 
11:43 PM
I am not here to rant agains C++, since I use it day-to-day, but I start hating the fact that almost every month I keep reporting compiler bugs etc
 
it's just a terrifically terrible idea.
@vsoftco That's what happens when they implement actual features.
 
user1804599
@vsoftco What compiler do you use?
 
@райтфолд g++
I need OpenMP
 
I came across this declaration: `std::unique_ptr<T>& ptr = std::make_unique<T>()`
What does the `&` do?
 
and compiling it on clang++ is a F-...-K pain in the butt
 
11:44 PM
@Puppy The return type would always be T&, not?
 
user1804599
@MichaelMitchell That's explained in the second or third chapter of your C++ book.
 
@MichaelMitchell Make it a reference?
 
user1804599
Go back to that section and read it again.
 
why would you want to make it a reference?
 
@Puppy yeah I guess that's true...
 
11:44 PM
what benefits does it have over declaring it without the ref
 
user1804599
Actually, it depends on what std::unique_ptr, T and ptr are.
 
@MichaelMitchell You should really re read that chapter in your book/tutorial.
 
@MichaelMitchell It makes your program not compile.
 
user1804599
It could be an assignment to &ptr which is compared to T.
 
user1804599
:P
 
11:45 PM
@StackedCrooked lol
 
user1804599
Also, for some reason, my code is broken.
 
@Pris what do you mean "then that's not a vector" - what do you think vector does when you emplace_back?
 
user1804599
(type3.fields().first + 0)->set(value3, value1);
(type3.fields().first + 1)->set(value3, value2);
REQUIRE((type3.fields().first + 0)->get(value3) == value1);
REQUIRE((type3.fields().first + 1)->get(value3) == value2);
 
user1804599
11:46 PM
This fails. :v
 
user1804599
Help!
 
however I'd just implement a "new" "++C" instead, where I'd get rid of all these annoying/hard-to-remember/hard-to-understand/un-compilable-for-most-compilers stuff. I know this is a HUGE project, D is a candidate since like forever, and still didn't make it.
 
@MichaelMitchell So it wouldn't be a copy.
 
@vsoftco lol let's call it C4
 
When you change the value of the reference, you also change the value of the thing it's referencing.
 
11:47 PM
yeah, sounds good
 
so, I understand why you would want to do this: for(auto& var : vector)
but why would you want to declare something new like that
is there at least a name for this so I can search it?
 
@MichaelMitchell wtf is that
automatic foreach?
 
range-based for
 
@Cinch for example, now you almost need a PhD to be able to write a fucking parser for C++, since < is so fucked up with templates
 
11:48 PM
sigh
 
@vsoftco Yeah C++ is pretty bloated.
 
then braced-initialization is just a failure
 
If you worry about the cost of copying PODs then just realize that move/forward can never help here. Compiler optimizations can help. Passing large POD by ref can help. (But passing small POD is often faster if you pass by value.)
 
auto takes it as a std::initializer_list
 
Can't we just create a new langauge called C! ?
 
11:49 PM
@melak47 I meant by using placement new, it wouldn't be me using a vector, it would be me manually allocating memory in a linear fashion and managing it. Vector might use placement new internally , but I don't see any way I can add a vector element that is a POD with aggregate initialization without copying.
 
and the most prefarable ctor is std::initializer_list one
 
@Pris you could, by making a custom allocator that uses a different construct when T is pod, I guess.
 
which, unless you read Scott Meyers latest book, you completely don't get why
 
Also, I was wondering about that masking language I had in mind
Like, let's create a binary storage language where you can define new data types really easy-like down to the bytes
 
auto x{1}
 
11:51 PM
@MichaelMitchell See here. Notice how when you add 5 to y (which is a reference to x), you end up adding 5 to x.
 
FUCK ME
 
@wilx in case you're interested, I wanted to link you to these the other day, but I only just found the links again: here and here and here
 
user1804599
Oh, I got the operands to std::memcpy in the wrong order.
 
@StackedCrooked Well, sure, but sticking in a std::move here really doesn't change anything.
@vsoftco I've been working on it for five years.
 
@Puppy nice to hear that, why don't you guys just change the bloody symbol?!
to something else?
not { or }
?
 
11:52 PM
I swear, C++ should not have references. It makes syntax soo hard.
 
not sure what you mean by "you guys", since I am the only developer working on my language.
 
@Puppy What language
 
@Cinch References have a use.
 
@Cinch lol references, they are just a drop in the syntactic ocean.
 
@Cinch Ruby.
 
11:52 PM
ahhh, I thought you worked on the comittee :)
 
@Nooble They do.
 
They should've used a different character than & though.
Maybe $.
 
the character in question is irrelevant.
even syntactically, what matters are it's syntactic properties, like typename ambiguity.
 
I don't remember Meyers' reasons for the different auto deduction but I guess it's to distinguish between constructors.
 
11:53 PM
I guess that $ wouldn't quite be typename ambiguous because $ is not a valid expression, unlike &.
 
@Puppy Functions in Wide don't have a return type?
 
@Nooble Inferred in most cases.
 
@Puppy It can be confusing because the C-style syntax and the C++-style syntax can often collide.
 
@Veritas Meyers' answer is basically this: "I am just puzzled"
 
@Cinch C's syntax collides with itself; C++ just exacerbates the problem a bit.
 
11:54 PM
Additionally, how would we feel about a language that can be either strong or weak-typed?
 
@Puppy, I actually LIKE a lot your stuff
 
using * for pointers has all the problems of & for references.
 
@Nooble ok, so if instead of an int we have like a string, when you initialize std::string str = std::string{"potato"};
 
from a syntactic perspective they're quite identical, which is probably why Bjarne picked it.
 
if you did std::string& str = std::string{"potato"}
 
11:55 PM
the question you'll always get from folks like us is "how fast the thing is" ?
 
what would happen differently
 
@vsoftco Could be faster than C++.
 
'cause otherwise I'd stick to Python
 
the copy constructor would not be called?
 
@MichaelMitchell You mean std::string("potato")?
@MichaelMitchell Wait does that even compile?
 
11:55 PM
either works
 
@Puppy can you briefly tell me why?
 
sure
 
@Cinch You described a weakly typed language. Make sure you don’t mistake static typing vs dynamic typing (which have formal meanings) with weak vs strong (which is more of a touchy-feely thing).
 
@MichaelMitchell Doesn't compile.
 
@Puppy like what's the main reason behind?
 
11:56 PM
does on MSVC
 
basically C++ has a bunch of rules that restrict how the compiler can optimize the program but they don't really benefit the user.
 
I don't think you can make references to anonymous objects.
 
@MichaelMitchell wow
 
you could define a new language that drops these rules, making it easier to optimize.
 
@MichaelMitchell When in doubt, MSVC is wrong.
 
11:57 PM
@Nooble You can- that's what rvalue references are. Not an lvalue ref though (at least not a non-const one without hoop jumping)
 
I had an idea for a language to help me with generics and low-level data management.
 
what exactly are we talking about compiling?
the {}?
 
@Puppy Oh. Never knew that.
 
@Puppy are you referring to any specific rules?
 
@vsoftco The main reason behind is that I'm sick of C++ tools that don't work and are terribly slow, and all the crap in the language that nobody needs.
 
11:57 PM
just curious about this stuff
 
@MichaelMitchell IIRC we are discussing BigO notation.
 
@Veritas Currently, I'm aware of at least structure layout rules (including base layout rules and things like EBO) and elision rules.
there are probably more to be found, like some integer overflow/underflow rules.
 
@Puppy, I see... that's interesting, do you have a specific typical example in mind? I know you can get rid of vtables etc (but writing them by yourself can make you slower), but what about TEMPLATES? I find them the best addition to C++ vs C, as they allow you to inline code (look at std::sort vs it's C-like qsort() countepart)
 

« first day (1617 days earlier)      last day (3330 days later) »