BigBahss / vscode-cmantic

C/C++ code generation for VS Code: Generate Definitions, Getters, Setters, and much more.
https://bigbahss.github.io/vscode-cmantic/
MIT License
84 stars 9 forks source link

[Feature Request] CopyConstructor / assigment operator #53

Open rperrot opened 1 year ago

rperrot commented 1 year ago

Hi,

first of all, thank you for your extension.

I have two suggestions for new features :

Given a class like that :

class Test 
{
  public:
  Test( void );
  Test( const Test & src );
  ~Test( void );

  Test & operator=( const Test & src );

  private: 

  std::string _value;
  std::string _value2;
};

It would be nice to have possibility to create definition for copy constructor and operator= :

Test::Test( const Test& src )
    : _value( src._value ),
      _value2( src._value2 )
{
}

And :

Test& Test::operator=( const Test& src )
{
  if ( this != &src )
  {
    _value  = src._value;
    _value2 = src._value2;
  }

  return *this;
}

These operations are frequently error prone (missing argument, wrong initialization) and may be encountered frequently in old project (prior to C++11).

Yours.

Shadouw commented 1 year ago

I suggest to additionally provide generators for

trws commented 11 months ago

I actually came by to mention a variation on this, it would even be useful to generate the block of standard defaults for all of these, even without definitions, to make it easy to explicitly default and/or delete constructors in a new class. This is actually one of my more time-consuming boilerplate tasks when working through modernizing code. For example, given the Test class in the OP , generate default definitions could generate this in either the header or (with the class-name prefix) the implementation:

Test() = default;
Test(const Test &) = default;
Test(Test &&) = default;
operator=(const Test &) = default;
operator=(Test &&) = default;
~Test() = default;

Almost the same for declarations would also be great, just without the = ..., for explicitly declaring that the constructors and destructor will be provided in an implementation file and avoiding the compiler generating them in all TUs:

Test();
Test(const Test &);
Test(Test &&);
operator=(const Test &);
operator=(Test &&);
~Test();

It feels like this would be easier than having to generate equivalent versions, and it would actually in some cases generate better code as long as the user can use c++11+.