1

I'm struggling with maybe a very simple hurdle but can't seem to understand how that works.

In my C++ project I want to provide lazy loaded classes. For this I thought of instantiating some fields of the type

std::shared_ptr<std::function<std::shared_ptr<MyClass>>> myClassLazyLoader;

At this point I'm not even sure anymore that is a valid approach, so please correct me if I'm completely off track.

I instantiate the field like this:

myClassLazyLoader = [] { return std::make_shared<MyClass>(); }

To invoke the function, I tried

myClassLazyLoader()

after which the compiler told me, as expected, that std::shared_ptr<std_function<... does not provide a call operator.

Naturally, I tried

(*myClassLazyLoader)()

but then it told me that std::function<... also does not provide a call operator which doesn't make any sense to me.

What am I doing wrong?

Thanks much in advance!

2
  • Can you extract and provide a minimal reproducible example? Also, the first attempt is obviously flawed, but the second one not so, so please include the exact and full error it produces. Commented Nov 8, 2021 at 16:45
  • 3
    You have a typo in template definition, missing () - it should be std::function<std::shared_ptr<MyClass>()>. Commented Nov 8, 2021 at 16:46

1 Answer 1

1

The moment I posted this I figured it out. The type should be

std::shared_ptr<std::function<std::shared_ptr<MyClass>()>>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.