Sandburg Sandburg - 5 days ago 5
C++ Question

Syntaxe C++11 « type var : var » is called « range-based for »

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 syntaxes? The rare one?

Recently I saw

int ackermann(m,n)
int m,n;
{
// function definition
}

Answer
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

Comments