Re:Why I don't spend time with Modern C++ anymore
by Ygg2 on Wednesday May 18, 2016 @07:42AM
(#11721193)
I had to write C++ yesterday and hated every second of it, from the clunky header files, to the errors that make no sense, to the can't make a cyclic dependency between classes i.e.
Long story short - I was to create a wrapper around a Poco::Runnable, so you can use the wrapper as a Poco::Runnable (don't ask why, it's TEH LAW) but without extending it.
[ Parent
| on HN ]
Re:Why I don't spend time with Modern C++ anymore
by xg15 on Wednesday May 18, 2016 @08:03AM
(#11721286)
C++noob here, but: Given that the members in your example are no pointers but actual substructures within your data structure, wouldn't that result in an infinite data structure? Therefore it sewms quite logical to me it's not allowed.
Disallowing cyclical dependencies via pointer would make no sense though.
Re:Why I don't spend time with Modern C++ anymore
by humanrebar on Wednesday May 18, 2016 @08:12AM
(#11721323)
Right. C++ objects are values, not references to values like in almost all other languages. So C++ needs to know the sizes of everything to construct them. If you tried to write out the mathematical series describing the ultimate size you would need to allocate for A or B, you would end up with a value approaching infinity.
Re:Why I don't spend time with Modern C++ anymore
by TheCoelacanth on Wednesday May 18, 2016 @10:49AM
(#11722354)
This is probably my favorite thing about C++. Values are so much easier to reason about than references.
Re:Why I don't spend time with Modern C++ anymore
by Ygg2 on Wednesday May 18, 2016 @01:19PM
(#11723646)
It's more of a simplification. Although, I did try references and they didn't compile either. In fact, if I remember correctly the error was "forward declaration forbidden".
After spending about 10-12 years away from C++, I was pretty much on novice level. I remember some things, but most details and day to day specifics are gone. It did serve as a fresh reminder why I hate C++. Or more precisely, why I hate C++ compilers.
Re:Why I don't spend time with Modern C++ anymore
by ygra on Wednesday May 18, 2016 @07:53AM
(#11721242)
You can only do that when using pointers or references:
class B;
class A {
B* b;
}
class B {
A* a;
}
because the compiler can (obviously) not tell how large the instances of the other class are when you have a field of that type. Remember that the class layout has to be fixed at compile time and both instance sizes depend on one another. That's not solvable.C# does the same, actually:
struct A {
B b;
}
struct B {
A a;
}
will cause the following error: test.cs(2,5): error CS0523: Struct member 'A.b' of type 'B' causes a cycle in the struct layout
test.cs(6,5): error CS0523: Struct member 'B.a' of type 'A' causes a cycle in the struct layout
with classes and references it works, of course: class A {
B b;
}
class B {
A a;
}
Re:Why I don't spend time with Modern C++ anymore
by Ygg2 on Wednesday May 18, 2016 @01:27PM
(#11723755)
This is also what I attempted, and as I remember it didn't work either, though to be honest I was really worn out fighting the obscure compiler errors by that point, I might have missed a sigil somewhere.
I spent good solid 30min trying to understand why defining and not defining my constructor was causing issues, to realize that the constructor error was actually a previous error, somewhere above the constructor.
Rust, also notes where and how you created an infinite loop.
Re:Why I don't spend time with Modern C++ anymore
by netheril96 on Wednesday May 18, 2016 @04:33PM
(#11725668)
Your compile error is likely because you use references instead of pointers. Reference as a class member is really hard to get right because it can be initialized only once, and cannot be copied or moved.
Re:Why I don't spend time with Modern C++ anymore
by humanrebar on Wednesday May 18, 2016 @07:54AM
(#11721250)
Not sure what you're trying to do, but:
class A; class B; // 'forward declaration' if you want to google
class A { unique_ptr<B> b; };
class B { unique_ptr<A> a; };
...generally classes instantiating each other is a sign that your design needs rearranged, but it has its uses when writing graph types.