Boost logo

Boost :

Subject: Re: [boost] [core] Determining interest for two new classes: readonly and newtype
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2015-08-17 12:30:03


Le 16/08/15 16:23, Sam Kellett a écrit :
> Hey, I have two classes I've developed that have been used in my own
> projects that I think could have a place in Boost. They are outlined below.
>
> readonly<T>:
>
> This is a wrapper class for a type to replace a const-type field in a
> movable class. It has const access to the type so can't modify except for
> the move constructor and move assignment operator.
>
> This solves the problem that no fields in a movable class can be const
> which can cloud the intentions of a field.
>
> readonly copies the accessor patterns seen in classes such as optional and
> the smart pointer collection.
>
> Example:
>
> struct foo
> {
> foo() : str("hello world") {}
>
> readonly<std::string> str;
> };
>
> foo f;
> auto g = std::move(f);
>
> static_assert(std::is_move_constructible<foo>{}, "");
>
> std::cout << *g.str << std::endl;
> g.str->clear() // error: non-const method.
How is this relatedto this proposal

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4388.html
> -----
>
> newtype<N, T>
>
> This is shamelessly ripped off from Haskell and provides strongly-typed
> typedefs to C++.
>
> It actually has two template parameters, one is an incomplete type that
> provides a unique identifier for this typedef. I've opted to use the name
> of the typedef with an '_t' suffix, the second is the raw type that it
> contains.
>
> Like readonly the underlying type can be accessed by operator*, operator->
> and get() methods.
>
> Example:
>
> using age = newtype<struct age_t, int>;
> age a{25};
>
> using byte = newtype<struct byte_t, int>;2]
> byte b{42};
>
> // do something with bytes (*not* any old int).
> void bar(const byte);
>
> bar(a); // error: wrong type wanted 'byte' got 'age'
> bar(b); // okay
> bar(89); // error: wrong type wanted 'byte' got 'int'
>
> assert(*b == 42);
>
> Any thoughts on the design and/or their place in Boost would be much
> appreciated.
>
>
You could take a look at the proposal for Opaque types [1].
I would be grateful if you can tell me what do you think of a prototype
I started longtime ago [2]. Please, note that it is not my intention to
discourage you with your proposal, as the scope of your class much simpler.

Best,
Vicente

[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3515.pdf
[2]
https://htmlpreview.github.io/?https://github.com/viboes/opaque/blob/master/libs/opaque/doc/html/index.html
https://github.com/viboes/opaque


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk