Followers 0

# c++11 iteration and warnings

## 26 posts in this topic

It's unfortunate that no single way of returning multiple values in C++ is really flexible, nice to write and safe/self-documenting.
On one hand, we have
typedef struct { int index; bool error; } foobar_return_t;
foobar_return_t foobar(){...}
auto result = foobar();
// do something with result.index, result.error - readable, little chance for mistakes
which would, IMO, be made more convenient if we were allowed to just write an anonymous struct in the function definition:
struct {int index; bool error;} foobar() {...} // but it's not allowed :-(
On the other hand, we have std::tuple which can conveniently assign straight into existing variables, which is kind of readable:
tuple<int,bool> foobar(){...}
tie(localInt, localError) = foobar();
but if the types are compatible or identical, we could easily assign or access them the wrong way.
I still like the latter approach for local lambdas where the function definition is visible from where it's being used.
0

##### Share on other sites

Other times you find yourself needing "throw-away" or "temporal" types -- types that exist maybe in one place, and so don't need to be shared around. You can typedef those too, but I don't think that usually buys any more semantic understanding either. Most times that you use an iterator is a good example of this, lambdas are another. In functional languages, the pattern is to create these kinds of temporal types all the time wherever they're needed, and you hardly even think about it that way.

You don't create "temporal" types in functional languages.  What are you talking about?

Its semantics I suppose, maybe "types" is a bit grandious a term if you come from a functional background (like I said, you hardly even think about it that way if you do), but if you were to perform similar patterns in, say, C++ before the auto keyword was introduced, then you'd have to name a new type to do it. Take pattern matching, for instance: you have a bunch of information, you care about some and ignore others, essentially each pattern is like (not is) a distinct type, which feeds through to the expression on the right. Or, you've got two homogenous lists and want to call a function that takes the information as a single, interspersed list, so you zip them together.

Its not so much that you're actually creating types, but you're constantly re-packaging information to look at it in different ways or to pass it to different functions. The point is, though, that in functional languages you're working with an algebra of types, and as long as its irrelevent you never really have to say what those types are, which is a very different notion than is typical of C++, even types defined by template parameters. Functiona languages don't fall apart just because of this "willy nilly" non-statement of types, and the reason that it *doesn't* fall apart is largely because the emphasis in those languages is on thinking about the properties of types, not just what information they hold, as is typical in C++.

0

## Create an account

Register a new account