c++ - Elegant way for "if(T t = ...) { } else return t;"? -
is there better way "idiom"?
if(state s = loadsomething()) { } else return s;
in other words, want something, may return error (with message) or success state, , if there error want return it. can become repetitive, want shorten it. example
if(state s = loadfoobar(&loadpointer, &results)) { } else return s; if(state s = loadbaz(&loadpointer, &results)) { } else return s; if(state s = loadbuz(&loadpointer, &results)) { } else return s;
this must not use exceptions favor otherwise (unsuitable build). write little class booleannegator<state>
stores value, , negates boolean evaluation. want avoid doing ad-hoc, , prefer boost/standard solution.
you do:
for (state s = loadsomething(); !s; ) return s;
but not sure if more elegant, , less readable...
Comments
Post a Comment