As for the break-based else, it solves a somewhat different issue, but that might also be useful. In fact, I like that idea very much (the way we work around it, i.e., what it'd save us, is a lambda - let alone function - that returns inside the loop and has fallback logic after the loop) - it's far from confusing, esp. in C++, when you're used to try-catch blocks. Perhaps a 'when(break 0) {}` and `when(0) {}` (or catch similarly) syntax could handle both. This is very similar to exceptions but it's way faster - in fact, given proper compiler support and inlining, it requires no additional instructions executed, just the jump addresses changed. In fact, there's no reason why we should restrict ourselves to only one of these.
From: D. B. Sent: Sunday, July 17, 2016 1:02 PM Reply To: std-pr...@isocpp.org Subject: Re: [std-proposals] Re: for ... else |
I have my doubts about whether the proposal is useful enough to spend time on,But I like 'for else' better than catch.But even better would be 'or else' :-)
Thanks! This is very promising, however, I miss the `catch empty` branch. With that, our syntax would look like: {for|while}(...) { ... } catch default ... catch break ... catch() ...
I leave it up for your votes whether we should use:
- catch
- else, {for|while} else
- case
- when (or any new keyword)
#define if_for(init,cond,incr) if(init ; cond)for(;cond;incr)
...
if_for(int i = 0 ; i < n ; i ++)
{
...etc...
}else{
cout << "cond was false\n";
}
tuple-for is asked by novice-to-medium-level programmers all the time. Yes, it's a templated body. I didn't write template<typename T> for( T ... ) as we have generic lambdas now, but I'm okay with that syntax as well. I'm aware of the 'templated for_each pattern', but I find it unnecessarily complicated (to explain, mainly) for such a trivial task. Also, I'm hoping to see 'heterogeneous container' visitors (think of boost::fusion) and variant visitors (similarly to for over optional).
Or am I missing something trivial that can already do these, perhaps with some library code?
2016. július 24., vasárnap 0:06:34 UTC+2 időpontban D. B. a következőt írta:...but then (and again, this assumes I'm inferring its purpose correctly) I don't know whether that pattern is common enough to be considered for a syntax extension, nor whether the existing ways to roll-your-own via variadic templates are difficult enough to justify it.
To be fair, it's not easy to write code that will be executed for each element in a tuple. It represents a hole in doing things with tuples.
It's easy to build a tuple. It's easy to pass tuples around. It's easy to get a single element from a tuple. These basic uses of a tuple do not require complex metaprogramming techniques.
But to perform an act, even if it's just a single function call, on each element requires metaprogramming. Granted, performing an act for each member of a struct requires... well, it requires macros & metaprogramming, so it's much worse. But with a tuple, it seems (from the perspective of a novice) to be such a simple thing.
Hence my desire for tuple-to-pack transformations. That's a simple thing that they can understand.
void function(std::vector<int> v)
{
for (int j = 0; j < 2; ++j) break_label(abc) {
// Choose exactly one of these by uncommenting:
// int i; for_if_nodecl(i : v) { // C++98
// int i; for_if(i : v) { // C++11
for_if(auto&& i : v) { // C++11
std::cout << " " << i;
if (2 < i && i < 5) { break; }
}
else if ( _empty ) { std::cout << " empty"; }
else if (!_break && _broken) { std::cout << " last"; }
else if (!_break &&!_broken) { std::cout << " done"; }
else if ( _break ) {
std::cout << " (break at " << *_begin
<< ", broken: " << _broken << ") ";
if (*_begin > 3) {
std::cout << std::endl;
break_to(abc);
}
continue_from_break;
}
std::cout << std::endl;
}
}