4 ms·
There are constexpr constructors
by VeryHacker 7y ago
There are constexpr constructors
- saagarjha 7y agoI'm looking for constructors that return immutable objects. Is there something in C++ that can help me?
- nurettin 7y agoConstructors do not return, and return type and lhs are decoupled, so you are looking for auto const &lol = make_wtf();
- saagarjha 7y agoWell, more like I’m looking for something that gives you a const that you can’t get rid of. You could use a factory method but it’d be nicer if I could have a constructor that did it.
- munchbunny 7y agoThat's just not how C++ is designed. If you want const, you stick const on the member variables or declare member getter functions const. Then the language checks your work when you initialize an object into a constant variable. This allows you to force an object to be immutable at a compiler enforced level regardless of how the calling code tries to initialize it.
- saagarjha 7y agoUnfortunately I am trying to wrap const to something that wasn’t designed this way: https://news.ycombinator.com/item?id=20948523 https://news.ycombinator.com/item?id=20948523
- nurettin 7y agoSince soft const constraint is not part of the type, perhaps you could pass it to a lambda function that treats it as a const and another lambda function that treats it as mutable. A sort of const region and mutable region.