这不是关于lambda函数的问题,我知道可以将lambda分配给变量。
允许我们在代码中声明但不定义函数的目的是什么呢?
例如:
#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;
}
那么我想知道的是,为什么C++允许看起来无用的two
和看起来更加复杂的three
,但不允许one
?
编辑:
从答案中可以看出,在代码声明中可能能够防止命名空间污染,但是我希望听到的是为什么允许声明函数的能力,而禁止定义函数的能力。
one
是一个函数定义,另外两个是声明。 - Some programmer dude