Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3029

Constructor with functional / construction via lambda

$
0
0
I have a class which upon construction has a part which should be executed when the constructor has finished "enough" to make the call valid.

Take the following simplified class example (the actual problem is much more elaborate):
Code:

#include <functional>

class base
{
public:
        base(int i, std::function<void()> func) : mi(i)
        {
                func();
        }

protected:
        void                foo() { }

        int mi;
};

class derived : public base
{
        derived(int i) :
                base(i, [&](){ foo(); })        // can capture with [&] or [this]
        {
        }
};

int main()
{
        // base b(14, [](){ foo(); });        // ??? doesn't compile
        return 0;
}

Explicitely creating a derived class allows me to properly implement the lambda to call a member of the class.

I can't seem to figure out how to do this for an explicit instantiation of the base class.

Can this be done ? Or is this a shortcoming of VS2012 or something the standard doesn't handle ?

Viewing all articles
Browse latest Browse all 3029

Latest Images

Trending Articles



Latest Images