Check reference passing when passing objects with heavy C++ generation and release to functions with const

Asked 2 years ago, Updated 2 years ago, 82 views

In C++, you may forget to refer to objects with heavy generation and release, such as std::set, when passing them to a function with const.Do you have any existing code review tools or compiler options to check this (now using GCC 7.3)?
Even if I'm careful, I'm in trouble because code reviews often warn me.

  • I would like you to give me answers other than "I'll be careful myself", but if that's the only way I can do it now, please let me know.
  • If you do not have such an existing tool for some reason, please let me know why.

c++ gcc compiler

2022-09-30 17:44

1 Answers

Make sure you turn off the copy constructor (and substitute operator).

For example, if you do not want to copy std::set<int>, define the class that inherited std::set<int> and clear the copy constructor and operator=.

class IntSet:public std::set<int>{
public:
  IntSet();
  // Define other constructors as needed

  IntSet(const IntSet&) = delete;
  IntSet&operator=(const IntSet&)=delete;
};

This causes a compilation error if a copy of an instance is required for an argument that is not a reference.

If you need to copy intentionally, add a member function such as IntSetclone()const.


2022-09-30 17:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.