Opened 9 years ago

Closed 9 years ago

#5766 closed defect (invalid)

Propose adding const to functions

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

Description

Not proposing this for 1.11.x as this may alter the binary ABI.

For functions like CSLLoad2, passing a const int to nMaxLines or int nMaxCols causes a warning or compilation failure for some compilers. Is there any reason that I should not add const to these types of args in svn trunk?

e.g. (slightly off diff - do not try to apply with patch)

Index: cpl_conv.cpp
===================================================================
--- cpl_conv.cpp        (revision 28096)
+++ cpl_conv.cpp        (working copy)
 
-const char *CPLReadLine2L( VSILFILE * fp, int nMaxCars, char** papszOptions )
-
+const char *CPLReadLine2L( VSILFILE * fp, const int nMaxCars,
+                           char** papszOptions )
 {
     (void) papszOptions;
 
Index: cpl_conv.h
===================================================================
--- cpl_conv.h  (revision 28096)
+++ cpl_conv.h  (working copy)
@@ -75,7 +75,7 @@
 char CPL_DLL *CPLFGets( char *, int, FILE *);
 const char CPL_DLL *CPLReadLine( FILE * );
 const char CPL_DLL *CPLReadLineL( VSILFILE * );
-const char CPL_DLL *CPLReadLine2L( VSILFILE * , int nMaxCols, char** papszOptions);
+const char CPL_DLL *CPLReadLine2L( VSILFILE * , const int nMaxCols, char** papszOptions);
 
 /* -------------------------------------------------------------------- */
 /*      Convert ASCII string to floationg point number                  */
Index: cpl_string.cpp
===================================================================
--- cpl_string.cpp      (revision 28096)
+++ cpl_string.cpp      (working copy)
@@ -293,7 +295,8 @@
  * @since GDAL 1.7.0
  */
 
-char **CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols, char** papszOptions)
+char **CSLLoad2(const char *pszFname, const int nMaxLines, const int nMaxCols, char** papszOptions)
 {
     VSILFILE    *fp;
     const char  *pszLine;
Index: cpl_string.h
===================================================================
--- cpl_string.h        (revision 28096)
+++ cpl_string.h        (working copy)
@@ -84,7 +84,7 @@
 
 int CPL_DLL CSLPrint(char **papszStrList, FILE *fpOut);
 char CPL_DLL **CSLLoad(const char *pszFname) CPL_WARN_UNUSED_RESULT;
-char CPL_DLL **CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols, char** papszOptions) CPL_WARN_UNUSED_RESULT;
+char CPL_DLL **CSLLoad2(const char *pszFname, const int nMaxLines, const int nMaxCols, char** papszOptions) CPL_WARN_UNUSED_RESULT;
 int CPL_DLL CSLSave(char **papszStrList, const char *pszFname);
 
 char CPL_DLL **CSLInsertStrings(char **papszStrList, int nInsertAtLineNo, 

Change History (4)

comment:1 by Kurt Schwehr, 9 years ago

Status: newassigned

comment:2 by Even Rouault, 9 years ago

I can't make sense of this. If you call CSLLoad2(const char *pszFname, int nMaxLines, int nMaxCols, char papszOptions) with a const int variable for nMaxLines, why should the compiler warn about this ? There's no way CSLLoad2() can modify the passed value from the point of view of the caller. The difference char* / const char* makes sense however, but not for a primitive type like int.

The following snippet compiles without any warning with gcc or clang in -Wall -Wextra

int foo(int bar)
{
    return bar;
}

int bar()
{
    const int i = 0;
    return foo(i);
}

comment:3 by Kurt Schwehr, 9 years ago

I was seeing a function identity mismatch, but now I'm not. So perhaps this is a lack of sleep.

comment:4 by Even Rouault, 9 years ago

Resolution: invalid
Status: assignedclosed

Closing that one then.

Note: See TracTickets for help on using tickets.