0

I want to execute a single operation multiple times, without defining a counter. For example, like this:

do(10)
{
    //do something
}

I think this would be useful in several different scenarios. For example:

  • Deleting several consecutive items from std::list by a beginning index.
  • Emitting some signal several times, either over time or at some specific time.
  • Adding the same data to the list for custom initialization Many scenes are not limited to those listed above.

Other languages have syntax similar to this that allows repeatedly executing the same command, without having to explicitly define a counter variable.In my opinion,defining a counter is completely inconsistent with human thinking.

Simulate how we think:

In reality, we always do sth. a few times directly.

But now the syntax looks like this:

  • Uh...I am going to do sth. three times.
  • Okay,ready,I started.

Soul torture: Why doesn't C++ provide a concise syntax? Although I am a fan of C++, I can’t help but wonder why some people don’t like C++ because C++ rarely considers how people think.I hope C++ can advance with the times and become the programming language of the future.

Different from Modern C++ way to repeat code for set number of times.I gave my plan, application scenarios, and even emotional appeals.

Crawl.W
  • 403
  • 5
  • 17
  • 9
    `for (int i = 0; i < 10; ++i)` ain't that bad is it? At least it *generalises*. – Bathsheba Apr 12 '19 at 14:53
  • You could put your loop behind a macro and get something like `TIMES_X(10){ ... }`, but I wouldn't recommnd it. – Blaze Apr 12 '19 at 14:55
  • 3
    `while (true) { /* do something */ }` does something multiple times. No counter in sight ;) – Jesper Juhl Apr 12 '19 at 14:55
  • 2
    What's the benefit of such syntax existing, opposed to the general form of `for`? – Algirdas Preidžius Apr 12 '19 at 14:55
  • @AlgirdasPreidžius it's shorter, it's more concise, it's more functional programming : – SergeyA Apr 12 '19 at 14:57
  • 1
    @SergeyA 1) What's the benefit of it being shorter, then? I thought such question, was implied in my question.. 2) concise is just a synonym for shorter. – Algirdas Preidžius Apr 12 '19 at 14:58
  • 1
    @SergeyA how is repeating an action functional programming like in anyway? If the loop body doesn't leave side effects, what is the point repeating it? – Ajay Brahmakshatriya Apr 12 '19 at 15:01
  • 2
    As far as I can tell, putting this into C++ exactly as the OP proposes would not be a breaking change. The ISO guys would hate it though. Could even be useful when initialising vectors. It also has the advantage of not leaking a counting variable into the loop body. – Bathsheba Apr 12 '19 at 15:02
  • I am curious about examples of other languages. From the top of my head I dont know any. Btw strictly speaking one could say that you cannot count without a counter. Even if something like you propose was present in c++, I am pretty certain that it would basically be just syntactic sugar for a do-while loop with a counter – 463035818_is_not_an_ai Apr 12 '19 at 15:17
  • 1
    @user463035818 COBOL: `Perform something 5 times.` Apple script: `repeat 3 times ... end repeat` – user7860670 Apr 12 '19 at 15:18
  • C++ does not provide that capability. You could make a class that encapsulates that behavior. Boost probably has something that already does that. If this is a capability you need, you'll need to create it in C++, or use a different language. Languages are just tools, and some are more well-suited for certain problem domains and jobs than others. – Eljay Apr 12 '19 at 15:27
  • 3
    Why would it exist? Can you come up with a single meaningful use case for such feature? It would make sense in kindergarten-grade "learning" languages for introducing children to programming. But what would be the purpose of this in any real industrial-grade language? Where would one possibly need to repeat *the same* action multiple times? I mean, I can *force* myself to think of some such examples, but they are so rare and artificial... It just doesn't justify introduction of a dedicated language feature. – AnT stands with Russia Apr 12 '19 at 15:27
  • @AnT I do real industrial grade programming and I find myself having to repeat the same action a definite number of times, sometimes just increasing some parameters, sometimes randomizing values, sometimes reading from an input... and not needing the index value at all. So the question for me makes sense. – mcabreb Nov 10 '21 at 09:02

3 Answers3

5

The evolution of C++ is by committee. In the simplest terms folk propose stuff and the committee accepts or rejects it.

Interestingly your suggestion

do (integral_expression)
{
}

would not be a breaking change. Note there's no while after the loop body or while adjacent to do. integral_expression is almost a production rule in C++ in case labels of switch blocks, although it could be run-time evaluable in this case. It could even lend itself to clean code in the sense that the equivalent

for (int i = 0; i < integral_expression; ++i)
{
}

introduces i into the loop body which can be inconvenient as it can shadow an existing i.

That said, thought is needed for the case where integral_expression is negative. Perhaps introduce unsigned_integral_expression not unlike what needs to be written as the size expression when declaring a variable length array in a reasonably common extension to standard C++?

If you want this feature in C++, then why not propose it?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Here is an example of C++ code that will do something a given number of times. Could also be done in C, but the functor would have to be a function pointer.

There may be a use case for this, but I'd think that for C++ programs the standard loop syntax would be preferable.

#include <iostream>
#include <functional>

static void Do(int count, std::function<void(int)> fn)
{
    while(count)
    {
        if (count > 0) --count;
        else if (count < 0) ++count;

        fn(count);
    }
}

int main()
{
    Do(10, [](int count) { std::cout << "Loop is at " << count << "\n"; });
}
Eljay
  • 4,648
  • 3
  • 16
  • 27
1

lambdas make a clean pure library implementation possible if needed:

template<typename F>
constexpr void repeat(std::size_t const n,F const& f){
   for (std::size_t i=0;i<n;++i)
       f();
};

int x{};
repeat(5,[&]{
   std::cout << ++x << std::endl;
});

such a proposal is likely to get discarded by the committee, unless greater reasons support it.

Red.Wave
  • 2,790
  • 11
  • 17