为什么我不能在另一个函数里面定义一个函数? [英] Why can't I define a function inside another function?

查看:329
本文介绍了为什么我不能在另一个函数里面定义一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个lambda函数问题,我知道我可以为一个变量赋值一个lambda。



允许我们声明,一个函数里面的代码?



例如:

  #include< ; iostream> 

int main()
{
//这是非法的
// int one(int bar){return 13 + bar; }

//这是合法的,但为什么要这样呢?
int two(int bar);

//这样得到的工作完成,但人很复杂
class three {
int m_iBar;
public:
three(int bar):m_iBar(13 + bar){}
operator int(){return m_iBar;}
}

std :: cout<<三(42) '\\\
';
return 0;
}

所以我想知道的是为什么C ++允许两个似乎没用,三个看起来更复杂,但不允许一个



声明可能能够防止命名空间污染,我希望听到的是为什么声明函数的能力已被允许,但是定义函数的能力已被禁止。

解决方案

这不明显为什么一个是不允许的;很久以前,在 N0295 中提到嵌套函数:


我们讨论嵌套函数到C ++的介绍。嵌套
函数很好理解,并且它们的介绍要求编译器供应商,程序员或委员会的
努力。 [...]


很明显,这个提案被拒绝了,在 1993 在网上有会议纪要。



我们没有可能的原因。实际上,此提议在 Lambda表达式和C语言的闭包
++


一篇文章[Bre88] C
++委员会[SH93]建议将嵌套函数添加到C
++中。嵌套函数类似于lambda表达式,但定义为函数体内的语句,并且除非该函数处于活动状态,否则不能使用结果
闭包。这些建议
也不包括为每个lambda表达式添加一个新类型,而
代替实现它们更像普通函数,包括
允许一个特殊类型的函数指针来引用它们。两个
这些提议先于添加模板到C
++,因此不要提及嵌套函数与通用算法的结合使用。此外,这些提议没有办法将
局部变量复制到闭包中,因此它们
生成的嵌套函数在它们的封闭函数之外是完全不可用的


$考虑到我们现在有lambdas,我们不太可能看到嵌套的函数,因为如纸的概述,它们是相同问题的替代方案,嵌套函数相对于lambdas有一些限制。



对于这部分问题:


  //这是合法的,但为什么要这样呢? 
int two(int bar);


有些情况下,这将是一个有用的方法来调用功能。草稿C ++标准部分 3.4.1 [basic.lookup.unqual]为我们提供了一个有趣的例子:

 命名空间NS {
class T {};
void f(T);
void g(T,int);
}

NS :: T parm;
void g(NS :: T,float);

int main(){
f(parm); // OK:calls NS :: f
extern void g(NS :: T,float);
g(parm,1); // OK:calls g(NS :: T,float)
}


This is not a lambda function question, I know that I can assign a lambda to a variable.

What's the point of allowing us to declare, but not define a function inside code?

For example:

#include <iostream>

int main()
{
    // This is illegal
    // int one(int bar) { return 13 + bar; }

    // This is legal, but why would I want this?
    int two(int bar);

    // This gets the job done but man it's complicated
    class three{
        int m_iBar;
    public:
        three(int bar):m_iBar(13 + bar){}
        operator int(){return m_iBar;}
    }; 

    std::cout << three(42) << '\n';
    return 0;
}

So what I want to know is why would C++ allow two which seems useless, and three which seems far more complicated, but disallow one?

EDIT:

From the answers it seems that there in-code declaration may be able to prevent namespace pollution, what I was hoping to hear though is why the ability to declare functions has been allowed but the ability to define functions has been disallowed.

解决方案

It is not obvious why one is not allowed; nested functions were proposed a long time ago in N0295 which says:

We discuss the introduction of nested functions into C++. Nested functions are well understood and their introduction requires little effort from either compiler vendors, programmers, or the committee. Nested functions offer significant advantages, [...]

Obviously this proposal was rejected, but since we don't have meeting minutes available online for 1993 we don't have a possible source for the rationale for this rejection.

In fact this proposal is noted in Lambda expressions and closures for C ++ as a possible alternative:

One article [Bre88] and proposal N0295 to the C ++ committee [SH93] suggest adding nested functions to C ++ . Nested functions are similar to lambda expressions, but are defined as statements within a function body, and the resulting closure cannot be used unless that function is active. These proposals also do not include adding a new type for each lambda expression, but instead implementing them more like normal functions, including allowing a special kind of function pointer to refer to them. Both of these proposals predate the addition of templates to C ++ , and so do not mention the use of nested functions in combination with generic algorithms. Also, these proposals have no way to copy local variables into a closure, and so the nested functions they produce are completely unusable outside their enclosing function

Considering we do now have lambdas we are unlikely to see nested functions since, as the paper outlines, they are alternatives for the same problem and nested functions have several limitations relative to lambdas.

As for this part of your question:

// This is legal, but why would I want this?
int two(int bar);

There are cases where this would be a useful way to call the function you want. The draft C++ standard section 3.4.1 [basic.lookup.unqual] gives us one interesting example:

namespace NS {
    class T { };
    void f(T);
    void g(T, int);
}

NS::T parm;
void g(NS::T, float);

int main() {
    f(parm); // OK: calls NS::f
    extern void g(NS::T, float);
    g(parm, 1); // OK: calls g(NS::T, float)
}

这篇关于为什么我不能在另一个函数里面定义一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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