Friday, 15 August 2014

c++ - Type design: value types, default-constructibility, optional and its relationship? -



c++ - Type design: value types, default-constructibility, optional<T> and its relationship? -

lately see lot of material generic programming, , still cannot wrap head around 1 thing, when designing types. not sure best way, allow me explain.

for types, natural provide default constructor. possible constructions of type valid or default makes sense, makes sense provide default. case basic types.

later, there types default constructing them not yield value. example, in standard library have std::function<sig> , std::thread, example. nevertheless, default-constructible, if not holding value.

later, have proposed optional<t> in standard. makes lot of sense utilize basic types, since basic types possible assignments represent valid value (except double , float nan), don't see how utilize thread or std::function<sig>, since these types don't hold "value" when constructed. if these types had "optional" embedded in type directly.

this has these drawbacks. since there no "natural" default (or value) construction, such int:

now have litter class if (valid) in design , signal error. or make less safe utilize if don't check. precondition -> assign before using if default-constructed.

so when want design type, find question: should create default constructible?

pros:

easier reuse in more generic contexts, because type more model semiregular or regular if add together appropiate operations.

cons:

litter whole class if statements or making contract user in class more unsafe use.

for example, let's have class song id, artist, title, duration , year. nice standard library create type default constructible. but:

i can't find natural way build "default song". i have litter if (validsong) or create unsafe use.

so questions are:

how should design type has no "natural (as in value)" defaults? should provide default constructor or not?

in case take provide default constructor, how optional<t> fit puzzle? view making type not "naturally" default constructible provide default constructor makes optional<t> useless in case.

should optional<t> used types domain of values complete, meaning, cannot assign invalid value representation because of them hold value, such in int?

why types such std::function<sig> made default constructible in first place in standard? when constructed, not hold value, don't see why default constructor should provided. do: optional<function<void ()>>, example. design selection , both valid or there 1 design, in case, choosing default vs non-default constructible superior other?

(note: problem lots of questions in 1 question parts of can duplicate. best inquire smaller questions, , check each prior posts. "one question per question" policy; easier said done sometimes, guess.)

why types such std::function made default constructible in first place in standard? when constructed, not hold value, don't see why default constructor should provided. do: optional<function<void ()>>, example.

see why std::function instances have default constructor?

how should design type has no "natural (as in value)" defaults? should provide default constructor or not?

default constructors types have tough time meaningfully defining without kind of info how lot of classes implement null value. optional improve choice? think so, i'm assuming you're aware std::optional voted out of c++14. if perfect reply can't everyone's answer...it's not soup yet.

it add together overhead runtime tracking of if value bound or not. perhaps not lot of overhead. when using language raison d'etre allow abstraction while still letting shoot in foot close metal want...shaving off byte per value in giant vector can important.

so if optional<t> semantics , compile-time checking perfect, still might face scenario it's advantageous scrap , allow type encode own nullity. gotta force pixels or polygons or packets or... pfafftowns.

in case take provide default constructor, how optional fit puzzle? view making type not "naturally" default constructible provide default constructor makes optional useless in case.

should optional used types domain of values complete, meaning, cannot assign invalid value representation because of them hold value (except float , double nan guess).

in own case, found myself wanting distinguish @ compile-time checking between routines handle null pointers , not. optional<pointer> offered situation of either optional beingness unbound, beingness bound null pointer, , beingness bound non-null pointer. compile-time sanity check seeming less win had.

so how optional references? they're controversial point lastly heard they're 1 of sticking points in set of things delayed std::optional c++14. bit annoying after i'd converted optional pointers optional references. :-/

i had vague thought write book "pathological c++" pick thought , start taking logical conclusions. optional<t> 1 kick got on , going principles identify. remove possibility of "nullity" beingness encoded in type itself, , can compiler doing type-checking whether given bit of code prepared expect null or not.

(these days tend toward suspecting if hung on kind of "pathological c++" you'll wind reinventing haskell. :-/ see popular data.maybe monad.)

c++ design optional default-constructor

No comments:

Post a Comment