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));
}
#define delete
to something else before including the header and#undef delete
afterwards? – Vanir#undef
. – Archival#define
a keyword. Most compilers support it as an extension though. – Madeira#define delete
is illegal. – Madeira