English 中文(简体)
how to cache a lambda in c++0x?
原标题:

I m trying to work with lambda s in C++ after having used them a great deal in C#. I currently have a boost tuple (this is the really simplified version).

typedef shared_ptr<Foo> (*StringFooCreator)(std::string, int, bool)
typedef tuple<StringFooCreator> FooTuple

I then load a function in the global namespace into my FooTuple. Ideally, I would like to replace this with a lambda.

tuplearray[i] = FooTuple([](string bar, int rc, bool eom) -> {return shared_ptr<Foo>(new Foo(bar, rc, eom));});

I can t figure out what the function signature should be for the lambda tuple. Its obviously not a function pointer, but I can t figure out what a lambda s signature should be. The resources for lambda s are all pretty thin right now. I realize C++0x is in flux at the moment, but I was curious about how to get this to work. I also realize there are simpler ways to do this, but I m just playing around with C++0x. I am using the Intel 11.1 compiler.

最佳回答

The -> operator sets the return type of the lambda, in the case of no return type it can be omitted. Also, if it can be inferred by the compiler you can omit the return type. Like Terry said, you can t assign a lambda to a function pointer (GCC improperly allows this conversion) but you can use std::function.

This code works on GCC and VC10 (remove tr1/ from the includes for VC):

#include <tr1/tuple>
#include <tr1/functional>
#include <tr1/memory>

using namespace std;
using namespace std::tr1;

class Foo{};
typedef function<shared_ptr<Foo>(string, int, bool)> StringFooCreator;
typedef tuple<StringFooCreator> FooTuple;

int main() {
    FooTuple f(
        [](string bar, int rc, bool eom) {
            return make_shared<Foo>();
        }
    );

    shared_ptr<Foo> pf = get<0>(f)("blah", 3, true);
}
问题回答

From Visual C++ Blog

I mentioned storing lambdas in tr1::functions. But you shouldn t do that unless it s necessary, as tr1::function has some overhead. If you want to reuse a lambda, or simply want to give it a name, you can use auto.

You should be able to store a lambda in a std::function. In your example, try storing it in a

std::function<std::shared_ptr<Foo>(std::string,int,bool)>

Don t forget about auto (although you won t be able to make an array of auto s, etc).





相关问题
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?

热门标签