为什么这个函子(“lambda”)会发出奇怪的警告?
当我使用 Visual C++ 2010 编译并运行它时:
#include <iostream>
int main() {
int subtrahend = 5;
struct Subtractor {
int &subtrahend;
int operator()(int minuend) { return minuend - subtrahend; }
} subtractor5 = { subtrahend };
std::cout << subtractor5(47);
}
我得到了正确答案 42。
然而,编译器抱怨这是不可能的:
Temp.cpp(9):警告 C4510:
main::Subtractor
:无法生成默认构造函数
Temp.cpp(6) :参见声明main::Subtractor
Temp.cpp(9):警告 C4512:
main::Subtractor
:无法生成赋值运算符
Temp.cpp(6) :参见声明main::Subtractor
Temp.cpp(9) :警告 C4610:
struct main::Subtractor
永远无法实例化 - 需要用户定义的构造函数
这是怎么回事?
When I compile and run this with Visual C++ 2010:
#include <iostream>
int main() {
int subtrahend = 5;
struct Subtractor {
int &subtrahend;
int operator()(int minuend) { return minuend - subtrahend; }
} subtractor5 = { subtrahend };
std::cout << subtractor5(47);
}
I get the correct answer, 42.
Nevertheless, the compiler complains that this is impossible:
Temp.cpp(9) : warning C4510:
main::Subtractor
: default constructor could not be generated
Temp.cpp(6) : see declaration ofmain::Subtractor
Temp.cpp(9) : warning C4512:
main::Subtractor
: assignment operator could not be generated
Temp.cpp(6) : see declaration ofmain::Subtractor
Temp.cpp(9) : warning C4610:
struct main::Subtractor
can never be instantiated - user defined constructor required
What's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
前两个警告只是让您知道由于存在引用数据成员而无法生成隐式声明的成员函数。
第三个警告是Visual C++ 编译器错误 。
所有三个警告都可以被忽略,不会产生任何不良影响,尽管您可以通过将引用数据成员设置为指针来轻松地使所有三个警告消失(引用数据成员几乎不值得麻烦)。
The first two warnings are just letting you know that the implicitly declared member functions cannot be generated due to the presence of a reference data member.
The third warning is a Visual C++ compiler bug.
All three warnings can be ignored with no ill effects, though you can easily make all three go away by making the reference data member a pointer instead (reference data members are almost never worth the trouble).
第一个警告是告诉您不能默认构造引用值(引用保证指向某个值)。将减数转换为常规整数,问题就会消失。
我很确定第二个警告具有类似的性质。
(只是说,通常最好依赖 boost::function 或类似的实现(std::tr1::function?),而不是手动编写此代码)
The first warning is to tell you that a reference value cannot be defaultly constructed(references are guaranteed to point to some value). Switch the subtrahend to a regular integer and the problem will go away.
I am pretty sure the second warning is of similar nature.
(Just saying, it is generally much better to rely on something like boost::function or a similar implementation(std::tr1::function?) instead of writing this code manually)
这是因为变量减法器5是一个未命名的结构体。如果您想让错误消失,请为减法器 5 所使用的结构命名。
例如:
不幸的是,我不太了解 C++ 语言,无法知道它为什么起作用,但我确实知道为什么会发生警告。
It's because the variable subtractor5 is an unnamed struct. If you want to make the errors go away, give the structure used for subtractor5 a name.
For example:
I unfortunately don't know enough C++ language-ese to know why it works, but I do know why the warning happens.
在以下情况下,用户定义的构造函数是必需的:
const int c_member;
)。int & r_member;
)具有其类型没有默认构造函数的数据成员。例如:
NoDefCtor 类
{
民众:
NoDefCtor(int);
};
类包含那个
{
NoDefCtor no_ctor_member;
};
从基类继承,基类没有默认构造函数。与上面几乎相同 (
NoDefCtor
)。A user defined constructor is mandatory in following cases:
const int c_member;
).int & r_member;
)Having a data member whose type doesn't have default constructor. Eg:
class NoDefCtor
{
public:
NoDefCtor(int);
};
class ContainThat
{
NoDefCtor no_ctor_member;
};
Inheriting from a base class, where base class doesn't have default constructor. Almost same as above (
NoDefCtor
).