简体   繁体   English

如何在C ++中绑定构造函数?

[英]How to bind a constructor in C++?

Needless to over explain. 无需过度解释。 The following code is self-evident: 以下代码不言而喻:

struct X
{
    X(int n){}
};

int main()
{
    std::vector<int> src;
    std::vector<X>   dest;

    // Below is not valid in current C++, but that is just what I want.
    transform(src.begin(), src.end(), back_insert(dest), std::bind(&X::X, _1)); 
}

A constructor takes some arguments and returns an object of the class of the constructor. 构造函数接受一些参数并返回构造函数的类的对象。

A constructor looks like a function, acts like a function, and is exactly a function. 构造函数看起来像一个函数,就像一个函数,并且只是一个函数。

So, I think std::bind should uniformly treat constructors and other callable objects. 所以,我认为std :: bind应该统一处理构造函数和其他可调用对象。

However, how can I extend the function template "bind" to implement that? 但是,如何扩展功能模板“bind”来实现呢?

Since X is implicitly constructible from int , a simple copy should achieve the conversion. 由于X可以从int隐式构造,因此简单的副本应该实现转换。

copy(src.begin(), src.end(), std::back_inserter(dest)); 

Or even using the vector's constructor: 甚至使用vector的构造函数:

std::vector<X>   dest(src.begin(), src.end());

In the general case, boost's lambda library has a constructor functor. 在一般情况下,boost的lambda库有一个constructor函数。

#include <boost/lambda/bind.hpp>
#include <boost/lambda/construct.hpp>
...
using namespace boost::lambda;
transform(src.begin(), src.end(), std::back_inserter(dest), bind(constructor<X>(), _1));

In this particular case binding might not be necessary, since there is only one argument to constructor. 在这种特殊情况下,绑定可能不是必需的,因为构造函数只有一个参数。

I don't know if there are cooler Boost ways, but you could write something like this: 我不知道是否有更酷的 Boost方式,但你可以这样写:

class Builder
{
    X operator() (int value) const { return X(value); }
};

transform(src.begin(), src.end(), back_insert(dest), Builder()); 

A constructor is a member function. 构造函数是成员函数。

A member function needs an object to be bound to. 成员函数需要绑定的对象。

A constructor is only called for a not-yet-existing object, so there can never be an object to bind the constructor to. 只为一个尚未存在的对象调用构造函数,因此永远不会有一个对象将构造函数绑定到。

In your case, the constructor arguments (the int values from src) should go to (a possible) back_inserter that calls the non-default ctor and not to std::transform. 在您的情况下,构造函数参数(来自src的int值)应该转到(可能的)back_inserter,它调用非默认的ctor而不是std :: transform。 (EDIT: this is only true for non-copy-ctor usage) (编辑:这仅适用于非复制用户)

Actually, what you want to do here is to call std::copy: 实际上,你想要做的是调用std :: copy:

std::copy(src.begin(), src.end(), std::back_inserter(dest)); 

A constructor takes some arguments and returns an object of the class of the constructor. 构造函数接受一些参数并返回构造函数的类的对象。

No, a constructor does not return anything, and there is no such thing as a pointer to a constructor. 不,构造函数不返回任何内容,并且没有指向构造函数的指针。

Build a factory function. 建立工厂功能。 These are static member functions or stand-alone functions that also construct objects, usually after validating inputs. 这些是静态成员函数或独立函数,通常在验证输入之后也构造对象。 They're useful whenever you want to ensure your objects cannot be built with invalid input (after all, you cannot abort construction within a ctor). 只要您想确保无法使用无效输入构建对象,它们就很有用(毕竟,您不能在ctor中中止构造)。

Since a factory is an ordinary function, you can easily bind to it to get the same effect you appear to want- a bound function that builds objects. 由于工厂是普通函数,因此您可以轻松地绑定它以获得您想要的相同效果 - 构建对象的绑定函数。

class X
{
public:
   X(OtherObject a, OtherData data);
   virtual ~X() {}
};

// Factory- return "X" instead of "X*" if not using the heap
X* CreateX(OtherObject a, OtherData data) {
    /*
        Logic that checks a, data for validity...
    */
    if(invalid) {
       return 0;   // You get no object
    }
    return new X();
}

Now you just bind to "CreateX" to build objects. 现在,您只需绑定到“CreateX”即可构建对象。 It is a normal function, however, not a constructor so all the normal rules for functions apply- especially those for copy and move construction of objects. 这是一个普通函数,但不是构造函数,因此函数的所有常规规则都适用 - 尤其是复制和移动对象构造的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM