English 中文(简体)
如何约束C++的建筑商?
原标题:How to bind a constructor in C++?

无需再作解释。 下述法典是不言自明的:

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)); 
}

构造者提出某些论点,并将之归为建筑商的一类物体。

施工者看着一种功能,像一种功能,而且实际上是一种功能。

因此,我想到的是:宾德应统一对待建筑商和其他可移动物体。

然而,我如何扩大“组合”的功能模板,以落实这一点?

最佳回答

Since X is implicitly constructible from int, a simple copy should achieve the conversion.

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

甚至使用病媒构造:

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

在一般情况下,加强lambda Library a

#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));

在这种具有约束力的案件中,可能没有必要,因为只有一种理由可以构造。

问题回答

我不知道有cooler。 更动的方法,但你可以这样写:

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

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

施工人员是成员的职能。

成员的职能需要受约束。

建造商只需要一个不存在的物体,因此永远不会有对建筑商具有约束力的物体。

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. (EDIT: this is only true for non-copy-ctor usage)

实际上,你想在此做的是: 副本:

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

构造者提出某些论点,并将之归为建筑商的一类物体。

无,建筑商不归还任何东西,也没有建筑商点。

建立工厂功能。 这些是固定成员职能或单独的功能,通常在确认投入之后也建造物体。 当你想确保不会以无效投入来建造你的物体时,这些物体会再次有用(毕竟,你不能在轨迹内拆除建筑)。

由于工厂是一种普通功能,你可以很容易地对工厂施加约束,使其产生与你似乎想要的形成物体的约束功能相同的效应。

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();
}

现在,你只是要“CreateX”来制造物体。 但这是一项正常职能,不是建筑商,因此适用所有正常职能规则,特别是复制和移动造物规则。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?