是否有内部函数的函数声明的使用? [英] Is there a use for function declarations inside functions?

查看:170
本文介绍了是否有内部函数的函数声明的使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以的声明的内部功能函数(我想一个局部变量,但它解析为函数声明):

We can declare functions inside functions (I wanted a local variable, but it parses as a function declaration):

struct bvalue;
struct bdict {
    bdict(bvalue);
}
struct bvalue {
    explict operator bdict() const;
}
struct metainfo {
    metainfo(bdict);
}
void foo(bvalue v) {
    metainfo mi(bdict(v)); // parses as function declaration
    metainfo mi = bdict(v); // workaround
                            // (this workaround doesn't work in the presence of explicit ctors)
}

是唯一的理由,因为它使分析器更简单和因为标准是这么说的,或者是有这个一个不起眼的使用?

Are the sole reasons "because it makes the parser simpler" and "because the standard says so", or is there an obscure use for this?

推荐答案

这真是一个C的问题,因为这种行为是直接从C(继承因为尽管它变得更preSS在C ++中的最让人头疼的解析的)。

This is really a C question, because this behaviour was inherited directly from C (although it gets much more press in C++ because of the most vexing parse).

我怀疑答案(在C的情况下,至少)是,这可以让你的范围函数声明为precisely需要它们的地方存在。也许这是有用的C.初期我怀疑任何人做任何更多的,但对于向后兼容性起见,不能从语言中删除。

I suspect the answer (in the context of C, at least) is that this allows you to scope the existence of your function declarations to precisely where they're needed. Maybe that was useful in the early days of C. I doubt anyone does that any more, but for the sake of backward compatibility it can't be removed from the language.

这篇关于是否有内部函数的函数声明的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆