c++ - workarounds for BOOST_THROW_EXCEPTION's missing ternary operator in initialization lists -


i end using ternaries throw exceptions may seem bit weird save day in initialization lists (hence helps writing sound constructors, hence helps raii, ...). e.g. if argument a smart_ptr<> want non nullptr, initiate member

member(a ? a->get_something() : throw exception()) 

i think valid, legit & safe use (do tell me if not).

i switched boost::exception, , unfortunately condition ? ret_value : boost_throw_exception(exception()) doesn't compile (since compiler cannot reify typeof(ret_value) , void).

are there work-arounds better creating whole new private static method , putting if inside?

i think valid, legit & safe use (do tell me if not).

it not, imo. can't , should not defend against crappy arguments get. because if would, have check everything. size_t function argument have checked against if contains sensible value. char* have checked if null or not, , if not you'd have check if zero-delimited. have apply thousands of checks throughout classes , functions check things unlikely happen happen in weird circumstances.

consider std::strlen , std::string::string(char const*): both demand argument non-null pointer null-terminated char string. no checks applied, if pass null, ub.

there many cases functions guarantee return non-null pointers. if such result passed constructor (or strlen, matter), check waste of time , programming effort.
in short: don't test null pointers, instead demand non-null pointers. it's responsibility of clients pass correct arguments, because client code knows if necessary check nullpointers, , has handle nullpointer case anyway, either checking before call or catching exception.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -