David Ledger
David Ledger

Reputation: 2101

C++ Construction of Static Lambda Member of Template type

I have a class that contains a static member, the member is a lambda. The lambda does have fixed arguments but might have different captures. This is where the problem comes in. This static member is not default constructible.

It sort of must be static too and I cannot use type erasure patterns as it is on an embedded system. Anybody have any ideas or patterns that solve this problem.

I can do in-place construction on a byte array of measured size but that creates its own problems and I want to avoid it.

What I have but don't like is:

EDIT 1 : Improved Example More complete example found here: https://wandbox.org/permlink/UMwsXSR6c2QYleiU

EDIT 2 : Further Improved Example Improved version of the above: https://wandbox.org/permlink/VTvGNlFRCNYG4J00

EDIT 3 : Fixed bug with return_t https://wandbox.org/permlink/tl4BaH1zbutMV2nU

Upvotes: 0

Views: 67

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 119847

You may want to do something like this

template <class T>
class StaticWrapper
{
    static T* t = nullptr;
    StaticWrapper(T&& tt) { 
        static T ttt {tt};
        t = &ttt;
    }
};

Same idea but no finicky casts and placement new.

Upvotes: 1

Related Questions