[gdal-dev] Mass addition of 'override' keyword ?

Andrew C Aitchison andrew at aitchison.me.uk
Fri Nov 25 07:48:55 PST 2016


On Thu, 24 Nov 2016, Even Rouault wrote:

> Hi,
>
> I've a branch where I've added the 'override' keyword to virtual methods that
> are an override of a virtual method declared in a base class.
>
> https://github.com/OSGeo/gdal/compare/trunk...rouault:add_override?expand=1
>
> Of course, not completely manually, but at >90% with the help of clang-tidy.
> GCC >= 5.1 has a -Wsuggest-override warning that I've enabled
> in configure to find all places missed by tidy-clang (at least with the drivers I've
> enabled in that branch). And recent clang versions have a less powerful
> -Winconsistent-missing-override (enabled with -Wall I think) that warns when
> you have explicitly tagged an overriden method with 'override' but neglected
> to do so for other overriden methods of the same class. So new code should
> be override friendly.
>
> As we still support C++03 compilers, the following tricks are used :
>
> 1) If the compiler doesn't advertize C++11 support, we #define override to empty in
> cpl_port.h. But that only if -DGDAL_COMPILATION is defined (which is the case
> in the Unix and Windows makefiles used to build GDAL), so as to avoid messing the
> namespace of code that includes GDAL.
>
> 2) As we don't want 'override' to leak into installed headers to keep C++03
> compat, then CPL_OVERRIDE is used in those files, instead of plain 'override'.
> It is a #define to override is the compiler is C++11 enabled, or #define to
> empty otherwise.
>
> As words may poorly reflect the reality of the code, here's the code:
> {{{
> #ifdef __cplusplus
>
> #if HAVE_CXX11 || _MSC_VER >= 1600
>
> /** To be used in public headers only. For non-public headers or .cpp files,
> * use override directly. */
> #  define CPL_OVERRIDE override
>
> #else
>
> /** To be used in public headers only. For non-public headers or .cpp files,
> * use override directly. */
> #  define CPL_OVERRIDE

We need an extra "#endif"
   #endif /* HAVE_CXX11 || _MSC_VER >= 1600 */
I think it goes here ?

> /* For GDAL source compilation only, ignore override if non C++11 compiler */
> #ifdef GDAL_COMPILATION
> #  define override
> #endif
>
> #endif /* __cpluscplus */
>
> }}}
>
> So in summary: use override in all .cpp files and non-installed .h headers.
> Use CPL_OVERRIDE in installed .h headers. Will make C++11 compilers enforce
> overriding, and C++03 ones ignore it.



More information about the gdal-dev mailing list