mardi 19 novembre 2019

Are there downsides with using std::tie for golang-like error-handling while also returning a result? (C++11)

In go a common way to do error handling and still return a value is to use tuples.

I was wondering if doing the same in C++ using std::tie would be a good idea when exceptions are not applicable.

like

std::tie(errorcode, data) = loadData();
if(errorcode)
  ...//error handling

Are there any downsides to doing so (performance or otherwise)? I suppose with return value optimization it doesn't really make a difference but maybe I'm wrong.

One potential problematic case that I could see is the use in a cross-compiler API but that's not specific to this use.

The current way I do this is

errorcode = loadData(&data);
if(errorcode)
  ...//error handling

but that allows to pass in a value for data.

The errorcode itself is something that is already defined and that I can't change.

Edit: I'm using/have to use C++11

Aucun commentaire:

Enregistrer un commentaire