Opened 6 years ago

Closed 3 years ago

#6164 closed task (wontfix)

Switch C++ code to using C++ version of C headers

Reported by: Kurt Schwehr Owned by: Kurt Schwehr
Priority: normal Milestone: closed_because_of_github_migration
Component: default Version: svn-trunk
Severity: normal Keywords:
Cc:

Description

To allow C++ code to better manage namespaces and do better error checking, it would be good to switch all C++ code to only use the C++ version of headers.

e.g. changing

#include <string.h>

to

#include <cstring>

The most pivotal location is in cpl_port.h. This file is included by most. The header will require using the preprocessor to switch the headers between C and C++.

  • cpl_port.h

     
    125125/*      Standard include files.                                         */
    126126/* ==================================================================== */
    127127
    128 #include <stdio.h>
    129 #include <stdlib.h>
    130 #include <math.h>
    131 #include <stdarg.h>
    132 #include <string.h>
    133 #include <ctype.h>
    134 #include <limits.h>
     128#ifdef __cplusplus
    135129
    136 #include <time.h>
     130#  include <cctype>
     131#  ifdef HAVE_DIRECT_H
     132#    include <cdirect>
     133#  endif
     134#  if defined(HAVE_ERRNO_H)
     135#    include <cerrno>
     136#  endif
     137#  include <climits>
     138#  ifdef HAVE_LOCALE_H
     139#    include <clocale>
     140#  endif
     141#  include <cmath>
     142#  include <cstdarg>
     143#  include <cstdio>
     144#  include <cstdlib>
     145#  include <cstring>
     146#  include <ctime>
    137147
    138 #if defined(HAVE_ERRNO_H)
    139 #  include <errno.h>
    140 #endif
     148#else /* This is C */
    141149
    142 #ifdef HAVE_LOCALE_H
    143 #  include <locale.h>
    144 #endif
     150#  include <ctype.h>
     151#  ifdef HAVE_DIRECT_H
     152#    include <direct.h>
     153#  endif
     154#  if defined(HAVE_ERRNO_H)
     155#    include <errno.h>
     156#  endif
     157#  include <limits.h>
     158#  ifdef HAVE_LOCALE_H
     159#    include <locale.h>
     160#  endif
     161#  include <math.h>
     162#  include <stdarg.h>
     163#  include <stdio.h>
     164#  include <stdlib.h>
     165#  include <string.h>
     166#  include <time.h>
    145167
    146 #ifdef HAVE_DIRECT_H
    147 #  include <direct.h>
    148 #endif
     168#endif /* __cplusplus versus c */
    149169
    150170#if !defined(WIN32)
    151171#  include <strings.h>

This will find a large number of the includes that need to be changed, but there are some false matches

find gdal -name \*.cpp -o -name \*.h | xargs egrep \
'["<](assert|float|limits|math|string|stdio|stdlib|signal|time|setjmp|errno|ctype|locale|stdarg|stddef|stdint|uchar|wchar)\.h'\
 | egrep -v 'cpl_config.h|libjpeg|libpng|libtiff|libgeotiff|degrib|libqhull|libjson|zlib|libcsf'

Roughly 110 files.

Change History (4)

comment:1 Changed 6 years ago by Even Rouault

For the sake of my curiosity, what advantages does it bring concretly ? (examples welcome)

comment:2 Changed 6 years ago by Kurt Schwehr

An example courtesy of Titus Winters is with abs. My quick demo of the surprise casting through int that can happen...

// no-cname.cc
#include <stdlib.h>
#include <iostream>

using std::cout;

float Foo() { return -1.23f; }

int main () {
  cout << abs(Foo()) << "\n";
  return 0;
}

Surprise...

$ ./no-cname 

1

With the cname header, abs is templated.

// cname.cc
#include <cmath>
#include <iostream>

using std::cout;

float Foo() { return -1.23f; }

int main () {
  cout << std::abs(Foo()) << "\n";
  return 0;
}

More what you might expect...

$ ./cname

1.23

comment:3 Changed 6 years ago by Even Rouault

OK thanks

comment:4 Changed 3 years ago by Even Rouault

Milestone: closed_because_of_github_migration
Resolution: wontfix
Status: newclosed

This ticket has been automatically closed because Trac is no longer used for GDAL bug tracking, since the project has migrated to GitHub?. If you believe this ticket is still valid, you may file it to https://github.com/OSGeo/gdal/issues if it is not already reported there.

Note: See TracTickets for help on using tickets.