News for hackers. Stuff that matters.
Re:Immer is a library of persistent and immutable data struc... by saagarjha on Wednesday September 11, 2019 @11:41PM (#20947713)
Related: it’d be great if C++ had const constructors, so that we could have true const objects.
Re:Immer is a library of persistent and immutable data struc... by leni536 on Thursday September 12, 2019 @01:56AM (#20948448)
What would be the utility of such a constructor? You can just initialize const objects with regular constructors. How are these objects not "true" const?
Re:Immer is a library of persistent and immutable data struc... by saagarjha on Thursday September 12, 2019 @02:12AM (#20948523)
The specific use case I had in mind was a implicit “conversion” constructor that took an immutable object and gave back something that you couldn’t strip the const off of (it’d also take a mutable object and give you a normal thing back). What I have specifically is a class that provides a base set of operations and a subclass that lets you do some modifications, and what I am trying to do is wrap it in C++ with an object that is const depending on which class I am converting (converting object to const wrapper_class and mutable_object, a subclass of object, to just wrapper_class).
Re:Immer is a library of persistent and immutable data struc... by leni536 on Thursday September 12, 2019 @03:45AM (#20949018)
I don't fully grasp what you are trying to do here. This looks familiar though:

> What I have specifically is a class that provides a base set of operations and a subclass that lets you do some modifications

If you actually do this with public inheritance then you possibly break const correctness.

Having to separate types for the immutable and mutable data structures with appropriate conversions is a sound idea though.

Re:Immer is a library of persistent and immutable data struc... by munchbunny on Thursday September 12, 2019 @01:30PM (#20954059)
> What I have specifically is a class that provides a base set of operations and a subclass that lets you do some modifications

Your object isn't actually immutable, it just doesn't let you directly mutate it. That's a really important distinction.

That's a common pattern. In C++, rather than using a subclass for the mutator, use a different (friend) class. The semantics for implicit conversion between two different classes will allow you to accomplish what you are asking for with enforcing no changes from one interface but not the other.

Re:Immer is a library of persistent and immutable data struc... by saagarjha on Thursday September 12, 2019 @10:47PM (#20958733)
> In C++, rather than using a subclass for the mutator, use a different (friend) class.

Unfortunately I don't control this class :(

Re:Immer is a library of persistent and immutable data struc... by VeryHacker on Wednesday September 11, 2019 @11:54PM (#20947792)
There are constexpr constructors
Re:Immer is a library of persistent and immutable data struc... by saagarjha on Thursday September 12, 2019 @12:38AM (#20948046)
I'm looking for constructors that return immutable objects. Is there something in C++ that can help me?
Re:Immer is a library of persistent and immutable data struc... by nurettin on Thursday September 12, 2019 @01:36AM (#20948355)
Constructors do not return, and return type and lhs are decoupled, so you are looking for

    auto const &lol = make_wtf();
Re:Immer is a library of persistent and immutable data struc... by saagarjha on Thursday September 12, 2019 @01:56AM (#20948445)
Well, 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.
Re:Immer is a library of persistent and immutable data struc... by munchbunny on Thursday September 12, 2019 @02:08AM (#20948501)
That'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.

Re:Immer is a library of persistent and immutable data struc... by saagarjha on Thursday September 12, 2019 @02:20AM (#20948571)
Unfortunately I am trying to wrap const to something that wasn’t designed this way: https://news.ycombinator.com/item?id=20948523
Re:Immer is a library of persistent and immutable data struc... by nurettin on Thursday September 12, 2019 @02:38AM (#20948652)
Since 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.