0

I have std::unordered_map<char, Argument&> and it works fine, but when I try to declare std::stack<Argument&>, I'm getting the following error:

In template: 'allocate' declared as a pointer to a reference of type 'ArgumentParser::ArgParser::Argument &'

4
  • 3
    Probably because a reference cannot be re-bound. Fortunately, reference_wrapper can be re-bound! Commented Nov 11, 2023 at 12:14
  • 1
    What is the original and underlying problem that you think will be solved by using references in containers? Normally one would store object themselves in the containers, and then use references to those objects. Perhaps your use-case might be better solved with std::shared_ptr<Argument>? Perhaps it can be solved some other way? Always please ask about the underlying and actual problem directly, and then tell us the solution you have in mind, and we can tell you if it's feasible or not, and possibly give you other and maybe better solutions. Commented Nov 11, 2023 at 12:28
  • 4
    In short, your question is an XY problem Commented Nov 11, 2023 at 12:29
  • 3
    The elements in your std::unordered_map are of type std::pair<char, Argument&>, which is not a reference type. Commented Nov 11, 2023 at 13:13

0

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.