c++ - When is it fair to purposefully cause undefined behaviour? -



c++ - When is it fair to purposefully cause undefined behaviour? -

the standard library habitually allows undefined behaviour if break requirements on template types, give erroneous function arguments, or other breach of contract. considered practise allow in user libraries? when fair so?

consider writing operator[] container:

template <typename t> t& container<t>::operator[](int i) { homecoming internal_array[i]; }

if i indexes outside bounds of internal_array, nail undefined behaviour. should allow happen or bounds checking , throw exception?

another illustration function takes int argument allows restricted domain:

int foo(int x) { if (x > 0 && x <= 10) { homecoming x; } }

if x not within domain, execution reach end of function without return statement - gives undefined behaviour.

should library developer sense bad allowing or not?

when fair purposefully cause undefined behaviour?

assuming you're asking point of view of library implementer: whenever warn client failing comply pre-conditions of given function causes undefined behavior, , client breaks pre-conditions.

the c++11 standard library defines lot of such functions: think of subscript operator sequence collections.

if you're asking viewpoint of application programmer, on other hand, reply of course of study "never", unless writing non-portable code relies on documented extension of compiler and/or on functionality of operating scheme (but it's arguable whether still "talking c++").

should library developer sense bad allowing or not?

if case, mr. stepanov should sense horrible now. no, not bad, depends on whether library designed maximum efficiency or maximum safety - lot of nuances in middle.

c++ libraries undefined-behavior contract

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -