Opened 7 years ago

Closed 7 years ago

#878 closed discussion (fixed)

implement move semantics conditionally

Reported by: Peter Owned by: Peter
Priority: major Milestone: yat 0.15
Component: build Version: trunk
Keywords: Cc:


I wanna implement move semantics (constructor and assignment) for some classes such as Matrix, Vector & Co, and BamRead. To do that we need c++11, so I suggest we look for a c++11 compiler in configure, turn on switches if needed etc, and #define some macro if YAT_HAVE_CXX11 (this macro already exist in the autoconf macro archive). Then we can use that to compile these constructors and assignment operators conditionally.

yat will still work with a c++98 compiler it is just slower for some cases when a c++11 compiler chooses to make a move, the c++98 compiler will make a copy.

Which leads us to the tricky part. The macro YAT_HAVE_CXX11 will be #defined or #undefined at configure when building yat so we can be sure that the macro reflects which compiler is used when builiding yat. What if the user does not use the same compiler when compiling his code against yat. For instance, with GCC 4.8.5, c++98 mode is default but if provided with -std=c++11 it is fully c++11 compatible so that's what yat's configure will turn on this switch. It is not unlikely that the user later compiles with g++ and since YAT_HAVE_CXX11 is #defined the compiler will see move constructor and other weird things from the future and die of confusion.

So we need some kind of guard for the user as well or at least say "hay yat was built with a cxx11 compiler but it looks like you are using a cxx98 compiler". The autoconf macro has the following header, from which we might steal some idea

#ifndef __cplusplus
#error "This is not a C++ compiler"
#elif __cplusplus < 201103L
#error "This is not a C++11 compiler"

Which compiler is used for yat is stored in yat-config so it's easy to modify one's Makefile and use that.

CXX = `yat-config --cxx`

yat's autoconf macro could be updated too. Perhaps a reasonable way is to let the header test compile against a header that contains YAT_HAVE_CXX11 guards as that will fail if CXX is a cxx98 compiler. Need to remember though, that the the macro should work against old versions of yat.

Change History (14)

comment:1 Changed 7 years ago by Peter

(In [3581]) refs #878

Make configure try to turn on compiler switches (such as -std=c++11), so it supports rvalues. If found, #define YAT_HAVE_RVALUE, which can be used both in source files and in header files. For header files, <yat/utility/config_public.h> must be #included.

As detailed in README users can turn off cxx11 support when at configure time of yat, or at compile time of user's package with preprocessor definition YAT_WITHOUT_CXX.

comment:2 Changed 7 years ago by Peter

(In [3582]) fix test failure, refs #878

comment:3 Changed 7 years ago by Peter

(In [3583]) move constructor and move assignment for omic::BamRead?; refs #878

comment:4 Changed 7 years ago by Peter

(In [3585]) move constructor and move assignment for utility::Matrix; refs #878

comment:5 Changed 7 years ago by Peter

Milestone: yat 0.x+yat 0.15
Owner: changed from Jari Häkkinen to Peter
Status: newassigned

comment:6 Changed 7 years ago by Peter

(In [3587]) move constructor for vector; refs #878 and #474

comment:7 Changed 7 years ago by Peter

(In [3588]) refs #878

Modify macro YAT_CHECK_HEADER so it adds -DYAT_WITHOUT_CXX11 if needed. It first tries compiling against yat/utility/Vector.h; it it fails, it retries with -DYAT_WITHOUT_CXX11. If that works, it means yat was built with c++11 support but the current compiler does not support, so we turn it off by adding -DYAT_WITHOUT_CXX11 to $YAT_CPPFLAGS.

Fixing two tests.

comment:8 Changed 7 years ago by Peter

(In [3589]) move assignment for Vector; refs #878

comment:9 Changed 7 years ago by Peter

I noticed in that packages break when built against yat trunk. I think it's too much of a breakage to allow this to be turned on by default. For 0.15 I suggest we change the default somehow so nothing breaks and push these features more aggressively in yat 0.16.

Should we change the default of configure option --with-cxx11; should we change the default behaviour of PP guards YAT_HAVE_CXX11? Or both? Considering that we want to change this in the future.

In the meantime yat.m4 needs some changes so it's ready for the 0.16 behaviour already now.

comment:10 Changed 7 years ago by Peter

(In [3593]) replace option --without-cxx11 with --enable-cxx11 and also change the default to 'disabled'. refs #878

comment:11 Changed 7 years ago by Peter

(In [3596]) move constructor and assignment in MatrixWeighted?. Remove declaration of assignment operator (not implemented), and add doxygen since tags. refs #878

comment:12 Changed 7 years ago by Peter

(In [3600]) move constructor aand assignmnet for BamHeader?; refs #878

comment:13 Changed 7 years ago by Peter

(In [3601]) introduce two macros, YAT_WITH_CXX11 and YAT_WITHOUT_CXX11, very much like GSL_RANGE_CHECK and GSL_RANGE_CHECK_OFF. If one of them is set, follow user's wish; if both are set, error out; if neither is set, c++11 is off. refs #878

comment:14 Changed 7 years ago by Peter

Resolution: fixed
Status: assignedclosed

(In [3602]) doc how to turn on cxx11 features. closes #878

Note: See TracTickets for help on using tickets.