msvc 是否有 gcc 的类似物 ({ })

发布于 2024-10-21 21:17:25 字数 336 浏览 0 评论 0原文

msvc 是否有 gcc 的类似 ({ })。

我认为答案是否定的。
请注意,这是编译器功能的问题,而不是品味或风格的问题。

我并不是建议任何人开始使用这个问题的 ({}) 结构。

对 ({}) 构造的引用为: http://gcc .gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC62 正式名称为“表达式中的语句和声明”。它允许将语句(如 for、goto)和声明嵌入到表达式中。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气尐潴 2024-10-28 21:17:25

在某种程度上,是的。这是一个复合语句表达式,可以将其视为 lambda 函数立即调用,并且只调用一次。

MSVC 的最新版本应该支持 lambda 函数,因此类似于:

[](){ /* your compound statement expression here */ }();

编辑:删除了多余的括号

编辑 2:为了您的娱乐,这里是一个如何将任一变体与一些(诚然完全愚蠢的)真实代码一起使用的示例。不要太在意代码的实际用处,而是它的表现力如何以及编译器甚至如何优化它:

#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;
}

... gcc 4.5 编译为:

movl    $2, 8(%esp)
movl    $2, 4(%esp)
movl    $LC0, (%esp)
call    _printf

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
半暖夏伤 2024-10-28 21:17:25

不,它不包含等效形式。

No, it does not contain an equivalent form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文