Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ reserved word as function pointer name in C struct

I suspect I know the answer to this already, but am curious if any experts out there have any tricks.

I have a C library built with the intention of being a C framework (unsafe functions unused, similar naming convention cross-platform, etc.). This works fine, up until I try to use it within a C++ project, where the C 'namespaced' function name conflicts with a C++ reserved word, in this case delete.

Here's the 'file' namespace, where I want it delete to be one of the function names:

#include <stdio.h>

#if defined(__linux__) || defined(BSD)
#   include <fcntl.h>
#endif


#if defined(__cplusplus)
#   if defined(_MSC_VER)
#       pragma warning ( push )
        // Disable: default constructor could not be generated
#       pragma warning ( disable : 4510 )
        // Disable: assignment operator could not be generated
#       pragma warning ( disable : 4512 )
        // Disable: struct <unnamed-tag> can never be instantiated
#       pragma warning ( disable : 4610 )
#   endif
extern "C" {
#endif



typedef struct
{
    int(*const close)(FILE* fp);
    int(*const copy)(const char* src, const char* dest);
    int(*const delete)(const char* path);
    int(*const flush)(FILE* fp);
    long(*const get_file_size)(FILE* fp);
    long(*const get_size)(const char* path);
    FILE*(*const open)(const char* name, const char* modes);
    int(*const path_exists)(const char* path);
    size_t(*const read)(void* ptr, size_t size, size_t count, FILE* stream);
    size_t(*const write)(const void* ptr, size_t size, size_t count, FILE* stream);

} namespace_file;

extern namespace_file const file;



#if defined(__cplusplus)
}   // extern "C"
#   if defined(_MSC_VER)
#       pragma warning ( pop )
#   endif
#endif

Now I'm writing some tests using gtest, and encounter the reserved word issue - is there anyway to bypass this, or shall I simply rename the function to purge or similar?

TEST(cfwkFile, fDelete)
{
    // all three of these error as 'delete' is reserved
    EXPECT_EQ(0, file.delete(CFWK_TEST_FAIL_FILE));
    EXPECT_EQ(1, file.delete(CFWK_TEST_PASS_FILE));
    EXPECT_EQ(1, file.delete(CFWK_TEST_PASS_FILE_COPY));
}
like image 519
ZXcvbnM Avatar asked Mar 16 '23 18:03

ZXcvbnM


1 Answers

"is there anyway to bypass this, or shall I simply rename the function to purge or similar?"

  int(*const delete)(const char* path);
          // ^^^^^^

Yes you should rename it, if the code in question is under your control!
You simply can't use a function pointer named delete in C++ compiled code, because it's a reserved keyword for the language.

You just need to make it different from the keyword. Instead of renaming it to purge, which might be misleading from a semantic level, you can still use something like

int(*const delete_)(const char* path);
              // ^

to get it distinct from the C++ keyword.


About your comments mentioning kind of asymetric/inconsistent naming:
Just use a _ postfix for all of them. As giving an opiniion, that's my preferred style for member variables of structs or classes anyway.

like image 107
πάντα ῥεῖ Avatar answered Apr 01 '23 05:04

πάντα ῥεῖ