-3

In an example regarding std::codecvt seen on http://en.cppreference.com/w/cpp/locale/codecvt, there was this syntax:

for (char16_t c : str16)

knowing that str16 is a variable (l-value) std::u16string str16.

What does it mean? That's what I call type var : var in the title.


PS: Is there a topic referencing all the particular syntax types? The rare ones?

Recently I saw:

int ackermann(m,n)
int m,n; 
{
    // function definition
}
Sandburg
  • 757
  • 15
  • 28
  • 5
    That is the syntax for a [range-based `for` loop](http://en.cppreference.com/w/cpp/language/range-for) – Cory Kramer Nov 28 '16 at 13:25
  • 2
    The syntax shown in your example is ancient (as in `int ackermann(m,n) int m,n; `), forget it honestly ( unless you're working with legacy code from the 80's/90's ). `for (char16_t c : str16)` is a range based for loop, it can be thought of as for each char16_t c in str16, in the definition of the loop `c` will be the current char. – George Nov 28 '16 at 13:26
  • That's a range-based for loop – Rerito Nov 28 '16 at 13:27
  • I have made researches. But, I was confronting a syntax, I didn't add any keywords giving me clues. My intuition tells me it is a `type var : var` not a `range-based for`. Plus, I tagged it `c++ ` not `c++11 `, so this question has its place here. – Sandburg Nov 28 '16 at 13:33
  • For a good reference on the syntax of various language constructs: http://en.cppreference.com/w/cpp/language . From there, you can click on link labelled range-for to find the explanation for the syntax you are asking about. – Klitos Kyriacou Nov 28 '16 at 13:35
  • 2
    `type var : var` is the syntax for a range based for loop, the syntax didn't exist in the standard pre C++11, it means create a new object of `type` and copy the current object to it. Also, if you've found code using ye'olde K&R syntax in the same code base as range based for loop syntax, then something is seriously wrong. – George Nov 28 '16 at 13:39
  • @george No it wasn't in the same code. It was to point out how it's difficult for me to find what a syntax mean, especially when old or deprecated. K&R and `range-based for` weren't mixed at all… :) – Sandburg Nov 28 '16 at 13:47
  • http://stackoverflow.com/documentation/c%2b%2b/7134/side-by-side-comparisons-of-classic-c-examples-solved-via-c-vs-c11-vs-c1/23914/looping-through-a-container#t=201611281350181849843, http://stackoverflow.com/documentation/c%2b%2b/7841/iteration#t=201611281350524510488 – Cody Gray - on strike Nov 28 '16 at 13:50
  • If you find my question very unpleasant or not useful, I can remove it. Not worth to make negative votes, it's also making my reputation going down, it's not fair. – Sandburg Nov 28 '16 at 16:29

3 Answers3

3
for (char16_t c : str16)

The syntax above defines a range-based for loop (introduced in C++11). It essentially says:

Loop over each character in str16, copy the character into a variable called c and allow me to use it.

For example:

for (char16_t c : str16)
{
    std::cout << c << std::endl;
}

Your second example is a K&R-style C function definition, and is obsolete. See here for more information: Function declaration: K&R vs ANSI

Community
  • 1
  • 1
Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
  • Thanks. It was also useful to have a link to the reference http://en.cppreference.com/w/cpp/language/range-for like given by @starl1qgt – Sandburg Nov 28 '16 at 13:48
0

This is called range-based for. This is very common idiom since C++11 for traversing whole container.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Starl1ght
  • 4,422
  • 1
  • 21
  • 49
0

for (char16_t c : str16) is the new ranged-for loop syntax introduced in C++11, plenty of examples and explanations around.

  • While mostly true (except for "new"), this answer is not useful, especially with the more helpful other answers – anatolyg Nov 28 '16 at 13:28
  • What do you mean by "except for new"? It's new in C++11. –  Nov 28 '16 at 13:35
  • I suppose you could say it _was_ new in C++11. Which is 5 (nearly 6) years old. And the syntax existed as a language extension in many compilers long before that. – Klitos Kyriacou Nov 28 '16 at 13:42