为什么这个函子(“lambda”)会发出奇怪的警告?

发布于 2024-12-01 02:18:45 字数 812 浏览 1 评论 0原文

当我使用 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 of main::Subtractor

Temp.cpp(9) : warning C4512: main::Subtractor : assignment operator could not be generated
Temp.cpp(6) : see declaration of main::Subtractor

Temp.cpp(9) : warning C4610: struct main::Subtractor can never be instantiated - user defined constructor required

What's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桃扇骨 2024-12-08 02:18:45

前两个警告只是让您知道由于存在引用数据成员而无法生成隐式声明的成员函数。

第三个警告是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).

独自唱情﹋歌 2024-12-08 02:18:45

第一个警告是告诉您不能默认构造引用值(引用保证指向某个值)。将减数转换为常规整数,问题就会消失。

我很确定第二个警告具有类似的性质。

(只是说,通常最好依赖 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)

土豪我们做朋友吧 2024-12-08 02:18:45

这是因为变量减法器5是一个未命名的结构体。如果您想让错误消失,请为减法器 5 所使用的结构命名。

例如:

struct subtractor {
 :
} subtractor5 = { subtrahend };

不幸的是,我不太了解 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:

struct subtractor {
 :
} subtractor5 = { subtrahend };

I unfortunately don't know enough C++ language-ese to know why it works, but I do know why the warning happens.

苏大泽ㄣ 2024-12-08 02:18:45

在以下情况下,用户定义的构造函数是必需的:

  • 初始化常量数据成员 (const int c_member;)。
  • 初始化引用数据成员 (int & r_member;)
  • 具有其类型没有默认构造函数的数据成员。例如:

    NoDefCtor 类
    {
    民众:
    NoDefCtor(int);
    };

    类包含那个
    {
    NoDefCtor no_ctor_member;
    };

  • 从基类继承,基类没有默认构造函数。与上面几乎相同 (NoDefCtor)。

A user defined constructor is mandatory in following cases:

  • Initializing constant data members (const int c_member;).
  • Initializing reference data members (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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文