« first day (4029 days earlier)      last day (902 days later) » 

1:10 AM
On the subject of shooting yourself in the foot, any good way to ensure two different structs are located in contagious memory within a structure? So that you can return a pointer to both through an std::span?
kinda thinking putting up some pragma packs might fix the issue
 
@Mikhail Yeah, if you pack each structure, then pack the structure containing them, you should end up with contiguous memory. For contagious memory, see "mandela effect".
 
somehow I still don't trust it
struct AClass
{
bool foo;
#pragma pack(push, 1)
SomethingA a;
SomethingB b;
#pragma pack(pop)
}
So in this case are SomethingA and SomethingB guaranteed to have no memory holes, or extra space between them?
 
 
1 hour later…
2:39 AM
Exemplary case of how to ask question right: stackoverflow.com/q/69732098/14768
 
 
4 hours later…
6:56 AM
ugg, getting sick of writting the same getter for the const ref and regular ref versions
 
 
2 hours later…
8:33 AM
Hello
 
World 👋🏾
 
 
1 hour later…
nwp
9:47 AM
@Mikhail I'm pretty sure that is UB no matter the memory layout. You can only do pointer arithmetic between objects of the same array and you don't have that.
 
 
7 hours later…
4:20 PM
@nwp I'm fairly sure you're correct mostly I think it's allowed if they are standard layout. But anything that's not 100% POD won't fit that bill.
 
@Mikhail How about 𝒂𝒏𝒐𝒕𝒉𝒆𝒓 struct? 🙃
 
The irony is Raymond has been talking about this lately devblogs.microsoft.com/oldnewthing/20211027-00/?p=105838
 
ended up doing something like

foo
{
std::array<BaseClass,2>
SomethingA getA();
SomethingB getB();
}
 
 
but now I need a macro to avoid gunking up my class with getters and setters
 
4:23 PM
(oof, delayed screenshot)
 
putting them in their class would basically be the same thing?
you still need to #pragma pack them
 
What do you mean?
 
so by #pragma pack, there can be a memory hole between a and b
 
Well the whole thing is 100% UB
 
@Mikhail How about pointers? Oh, wait...
std::tuples?
 
4:34 PM
foo
{
std::array<BaseClass,2> holder;
SomethingA& getA(){return (SomethingA&) holder[0];};
SomethingA& getA(){return (SomethingA&) holder[1];};

}
but its not too fun to write that kinda thing
also need 3 getters for const correctness
 
I mean you can const_cast if you really want to
 
so right now I'm trying to figure out a #define that will help avoid explicitly writing the 3 getters per each member (regular reference, const reference, the static implementation)
 
@Mgetz I'm guilty of doing that without knowing the possible downsides 😔
 
@Mikhail I'm still trying to figure out what you're doing and why this is so important as it's very much UB
 
yeah it wasn't well explained
Foo
{
std::array<BaseClass,2> holder;
std::span<BaseClass> get_items();//Interface requires a contiguous pointer of the base class
SomethingA& getA(){return (SomethingA&) holder[0];};// SomethingA has a bunch of template information but no members outside of the base class, aka sizeof(SomethingA) == sizeof(BaseClass)
SomethingB& getB(){return (SomethingB&) holder[1];};
}
basically my interface requires returning non-owning std::spans
 
4:38 PM
@Mikhail so see the link to raymond's blog above...
@Mikhail yeah this is extremely UB
you're slicing all over the place
 
nothing UB about returning non owning spans, unless they are possibly to the same memory location in the same context (aliasing rules)
 
when it's not actually a contiguous container? Yes that's UB
 
@Mikhail Again, how about std::tuples?
 
oh but in the code I shared they are contiguous in the sense they are both in an std::array
so not sure if its UB if they are separate members in a pragma (the original thing I shared)
 
well if you're addressing a base class as a derived when you've only allocated the base class that's UB
 
4:41 PM
 
but in this case the derived class has no new members or virtual functions (it just dumps in a bunch of template parameters). So I'm not sure if its UB
 
@Mikhail You have to use #pragma pack? 🤔
@Mikhail You can inherit the derived class.. struct MyType : contiguous<A, B>
 
it's still UB AFAIK
even if its standard layout
 
whats the UB part though?
 
@Mikhail well the most obvious is you're creating a lifetime for the base and then accessing it through derived which doesn't have a lifetime
 
4:54 PM
Object slicing?
Honestly my first idea was a malloc(sizeof A + sizeof B), but that's essentially kind of like a struct at the end of the day
 
I'm getting the reference to the derived by casting, so it has the lifetime restrictions of a reference
 
so at a minimum unless static_assert(std::is_standard_layout_v<SomethingA> && std::is_standard_layout_v<SomethingB>) passes you're in seriously ugly territory
 
I mean the two need to have the layouts (the layout of the base) but I don't see why they'd need to have standard layouts...
 
because you're aliasing
 
If SomethingA or SomethingB contains more data than BaseClass, wouldn't that be a problem?
 
4:58 PM
so
Struct Holder
{
F foo;
F& get_foo() {return foo};
}
does that trigger aliasing issues?
cause its possible to have an instance of the holder and reference to its member, which would point to the same address
 
Only if `get_foo()` `return`s a reference to a `struct` that contains more/ less data than `F`, I guess
or if the `return`ed `struct` isn't related to `F` 🤷🏾‍♂️
 
I just seems that the struct Holder thing I wrote shouldn't cause UB because having an object and reference to its member is a form of aliasing but also quiet common
although I guess you could

Holder a;
auto& ref1 = a.get_foo();
auto& ref2 = a.get_foo();
auto s = [ref1,ref2]{return ref1+ref2;}();
in that lambda the compiler doesn't know if ref1 and ref2 alias (or rather assumes they usually don't)
but UB due to aliasing doesn't happen when you make the reference, it happens when you start touching the data. And really the compiler assumes they data doesn't alias (so if it really aliases you're in trouble).
 
this might actually be a valid case for std::launder
 
5:19 PM
launder usually deep copies :-)
 
It does? 😯
 
I mean the compiler needs to issue different instruction if the memory locations alias, and there is no C++ way to communicate that the locations alias. C++ has 3 comments about aliasing 1) most cases assume no aliasing 2) char* may alias 3) [[restrict]] can be applied to certain kinds of objects to assume no aliasing
but to actually solve the alias you'd need to deep copy
 
6:16 PM
also we need static type checking in macros
#define MIXIN(foo : type) something(foo)
fudge, I've been doing too much python
 
6:37 PM
@Mikhail no it doesn't
it's basically just a magic pointer cast
if you looked at the definition you'd notice it's just a pointer thing
 
Yeah I think I'll need to look harder at std::launder
Then std::launder(p) returns a value of type T* that points to the object X. Otherwise, the behavior is undefined.
so maybe it doesn't solve anything :-)
 

« first day (4029 days earlier)      last day (902 days later) »