Does msvc have analog of gcc's ({ }).
I assume the answer is no.
Plase note that this is question of compiler capabilities, not question of taste or style.
Not that I recommend anybody to start using the ({}) construct by ths question.
The reference to ({}) construct is: http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC62 officially called "Statements and Declarations in Expressions". It allows to embed statements (like for, goto) and declarations into expressions.
In some way, yes. This is a compound statement expression, which one could consider like a lambda function that is immediately called, and only called once.
Recent versions of MSVC should support lambda functions, so that would be something like:
[](){ /* your compound statement expression here */ }();
EDIT: removed a surplus parenthesis
EDIT 2: For your amusement, here is an example of how to use either variation with some (admittedly totally silly) real code. Don't mind too much the actual usefulness of the code, but how expressive it is and how nicely the compiler even optimizes it:
#include <string.h>
#include <stdio.h>
int main()
{
unsigned int a =
({
unsigned int count = 0;
const char* str = "a silly thing";
for(unsigned int i = 0; i < strlen(str); ++i)
count += str[i] == 'i' ? 1 : 0;
count;
});
unsigned int b =
[](){
unsigned int count = 0;
const char* str = "a silly thing";
for(unsigned int i = 0; i < strlen(str); ++i)
count += str[i] == 'i' ? 1 : 0;
return count;
}();
printf("Number of 'i' : %u\t%u\n", a, b);
return 0;
}
... which gcc 4.5 compiles to:
movl $2, 8(%esp)
movl $2, 4(%esp)
movl $LC0, (%esp)
call _printf
No, it does not contain an equivalent form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With