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

[Feature Request] CopyConstructor / assigment operator #53

Open
rperrot opened this issue Oct 28, 2022 · 2 comments
Open

[Feature Request] CopyConstructor / assigment operator #53

rperrot opened this issue Oct 28, 2022 · 2 comments

Comments

@rperrot
Copy link

rperrot commented Oct 28, 2022

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
Copy link

Shadouw commented Sep 14, 2023

I suggest to additionally provide generators for

  • move constructor
  • move assignment operator
  • destructor

@trws
Copy link

trws commented Oct 20, 2023

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+.

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

3 participants