Sandburg Sandburg - 1 year ago 69
C++ Question

C++11 syntax 'type var : var' is called 'range-based for'

In an example regarding

seen on, 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

Answer Source
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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download