Is there a use for function declarations inside functions?

前端 未结 4 1276
时光取名叫无心
时光取名叫无心 2020-12-01 21:06

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

struct bvalue;
struct bdict {
    bd         


        
4条回答
  •  长情又很酷
    2020-12-01 21:44

    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).

    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.

提交回复
热议问题