Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const-correctness improvement suggestion #154

Closed
MaciejPatro opened this issue Dec 27, 2021 · 2 comments
Closed

const-correctness improvement suggestion #154

MaciejPatro opened this issue Dec 27, 2021 · 2 comments

Comments

@MaciejPatro
Copy link
Contributor

Hello,

  /* Getter enabled for all template types other than std::vector and std::list   
   * @throws std::logic_error in case of an invalid argument name                 
   * @throws std::logic_error in case of incompatible types                       
   */                                                                             
  template <typename T = std::string> T get(std::string_view aArgumentName) {     
    return (*this)[aArgumentName].get<T>();                                       
  }                                                                               

Function is providing a copy of value - but is not const - which leaks this to other classes that have ArgumentParser as a member.

Example of problem:

struct program_arguments
{
  int age()  /* const is not possible here */ { return parser.get<int>("age"); }
  argparse::ArgumentParser parser;
};

There seems to be no reason besides usage of [] operator for the function not to be const. It could be made const for example by using .at on std::map

@skrobinson
Copy link
Contributor

Was this addressed in #74 previously? Or have you found a new issue?

@MaciejPatro
Copy link
Contributor Author

Yes, you are fully right - sorry for the confusion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants