将 mem_fun 存储在标准容器中

发布于 2024-09-17 18:59:49 字数 213 浏览 3 评论 0 原文

有没有办法创建一个向量 mem_fun_t<返回类型,MyClass> > ?

我看到的错误是:

error C2512: 'std::mem_fun1_t<_Result,_Ty,_Arg>' : no appropriate default constructor available

Is there a way to create a vector< mem_fun_t< ReturnType, MyClass > > ?

The error i'm seeing is:

error C2512: 'std::mem_fun1_t<_Result,_Ty,_Arg>' : no appropriate default constructor available

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

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

发布评论

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

评论(3

亽野灬性zι浪 2024-09-24 18:59:49

我真的不明白为什么它不起作用,但这实际上是一个非常丑陋的解决方案。只需采用 vector> 即可避免 C++03 绑定程序中存在的这些问题。

I really can't see why it would not work, but it's actually a pretty ugly solution. Just take vector<function<ReturnType(MyClass*)>> and be without those issues present in C++03 binders.

千仐 2024-09-24 18:59:49

你当然可以创建这样一个向量。

#include <vector>
#include <functional>
#include <iostream>

struct MyClass
{
    int a()  { return 1; }
    int b()  { return 2; }
};

int main()
{
    std::vector<std::mem_fun_t<int, MyClass> > vec;
    vec.push_back(std::mem_fun(&MyClass::a));
    vec.push_back(std::mem_fun(&MyClass::b));
    MyClass x;
    for (size_t i = 0; i != vec.size(); ++i) {
        std::cout << vec[i](&x) << '\n';
    }
}

如果您遇到问题,请仔细阅读错误消息。例如,std::mem_fun 可以返回各种包装器,具体取决于您传递给它的内容。

或者确实,切换到 boost 或 C++0x 的 function


编辑:对于这个特定的错误消息,我假设您正在执行某些操作来调用包含类型的默认构造函数(例如 resize 或使用向量的构造函数指定大小)。您无法使用这些功能。

You certainly can create such a vector.

#include <vector>
#include <functional>
#include <iostream>

struct MyClass
{
    int a()  { return 1; }
    int b()  { return 2; }
};

int main()
{
    std::vector<std::mem_fun_t<int, MyClass> > vec;
    vec.push_back(std::mem_fun(&MyClass::a));
    vec.push_back(std::mem_fun(&MyClass::b));
    MyClass x;
    for (size_t i = 0; i != vec.size(); ++i) {
        std::cout << vec[i](&x) << '\n';
    }
}

If you are having problems, read the error message carefully. For example, std::mem_fun can return all sorts of wrappers, depending on what you pass to it.

Or indeed, switch to boost's or C++0x's function.


Edit: With this particular error message, I assume that you are doing something that invokes the default constructor for contained type (e.g resize or specifying the size with the vector's constructor). You can't use those functions.

栀梦 2024-09-24 18:59:49

mem_fun_t 满足存储在容器中的要求(可复制构造且可赋值),所以答案是肯定的。

但是,它不是默认构造的或可比较的,因此您无法使用它们的容器执行某些操作,包括:

  • 调整大小,除非您提供一个值来填充
  • 使用非零大小构造,除非您提供一个值来填充
  • 比较容器

您看到的错误来自于尝试调整大小或使用大小进行构造。

mem_fun_t meets the requirements to be stored in a container (it is copy-constructible and assignable), so the answer is yes.

However, it isn't default-constructible or comparable, so there are some things you can't do with a container of them, including:

  • Resizing, unless you provide a value to fill with
  • Constructing with a non-zero size, unless you provide a value to fill with
  • Comparing containers

The error you are seeing comes from trying to either resize, or construct with a size.

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