netcdf-cxx4 icon indicating copy to clipboard operation
netcdf-cxx4 copied to clipboard

C++11 Move Constructors

Open citibeth opened this issue 9 years ago • 7 comments
trafficstars

When using C++11, move constructors are needed for NcFile and other relevant classes. The following should work, but doesn't.

class NcIO {
    netCDF::NcFile _mync;
public:

    NcIO(std::string const &filePath, netCDF::NcFile::FileMode fMode) :
        _mync(netCDF::NcFile(filePath, fMode)) {}
};

citibeth avatar Jan 07 '16 20:01 citibeth

I agree that move constructors would be a nice feature. However, your code example could easily be rewritten to work without them:

...
  NcIO(std::string const &filePath, netCDF::NcFile::FileMode fMode) :
      _mync(filePath, fMode) {}
};

jarlela avatar Jan 08 '16 08:01 jarlela

I should know better, that was embarrassing...

On Fri, Jan 8, 2016 at 3:37 AM, Jarle Ladstein [email protected] wrote:

I agree that move constructors would be a nice feature. However, your code example could easily be rewritten to work without them:

... NcIO(std::string const &filePath, netCDF::NcFile::FileMode fMode) : _mync(filePath, fMode) {} };

— Reply to this email directly or view it on GitHub https://github.com/Unidata/netcdf-cxx4/issues/25#issuecomment-169934714.

citibeth avatar Jan 08 '16 14:01 citibeth

Move constructors would be very nice anyway to use it with the auto keyword

auto file = netcdf::ncFile(fname, netcdf::ncFile::read);

Currently this code fails because the copy constructor is private and no move constructor is generated.

kain88-de avatar Feb 14 '16 16:02 kain88-de

I'd be quite happy to work on this feature if it's still wanted. I'm currently using this in a C++17 environment and this, among other modern c++ niceties, is near the top of my wishlist.

htmlboss avatar Jul 16 '18 16:07 htmlboss

@htmlboss that would be fantastic, we'd welcome the contribution :).

WardF avatar Jul 17 '18 22:07 WardF

Finally have some time to attack this...I see 2 ways of going about this thing:

  1. Test against the __cplusplus macro:
#if __cplusplus >= 201103L
#define HAS_CXX11
#endif

and then:

#ifdef HAS_CXX11

// Move constructor
NcFile(NcFile&&) = default;

// Move assignment
NcFile& operator=(NcFile&&) = default;

#endif

That's your move-semantics 😸

This is really quick and easy, but some compilers may not adhere to correctly setting the __cplusplus macro, although it's defined in the c++ standard.

  1. Doing some magic with autotools. Not really familiar with it but I quickly found out it can reliably test for specific c++ versions.

@WardF what's your opinion?

htmlboss avatar Aug 29 '18 23:08 htmlboss

I'm concerned about accidentally boxing out some developers because of a compiler they're using, but I think most modern compilers support setting the __cplusplus macro. Lets go with option 1 since it should be the easiest way forward. If we receive a complaint, it should be easy enough for us to move the check into autotools and cmake; I found the cmake info here: https://stackoverflow.com/questions/10984442/how-to-detect-c11-support-of-a-compiler-with-cmake

Thanks again for your help with this!

WardF avatar Aug 30 '18 21:08 WardF