Function naming conventions mangling / overloading in c

Lets say that I have multiple functions that accomplish the same goal. An example of this would be creating a 'constructor' for a data type.

String new_String();
String new_String(const char *cstr);
String new_String(String s);

Obviously this cant be accomplished in C, but with functions like these (and situations where function overloading is useful) is there a convention or best practice for naming them?

Something like this?

String new_String();
String new_String_c(const char *cstr);
String new_String_s(String s);

which to me feels awkward and not easy to read. Or something like this?

String new_String();
String new_String_from_cstr(const char *str);
String new_String_copy(String s);

Which reminds me of horribly long java names because this could soon get ridiculous.

int String_last_index_of_any_characters(String s, char *chars, int length);

Answers


You can define a single "constructor" that takes a void * initialization parameter and an enum:

enum StringCtor { SC_DEFAULT, SC_C_STR, SC_COPY };

String new_String(enum StringCtor type, const void *arg);

String s1 = new_String(SC_DEFAULT, 0);
String s2 = new_String(SC_C_STR, "hello");
String s3 = new_String(SC_COPY, &s2);

You could also choose to use a ... instead of a void *. The idea is the same, but you use the va_* macros to extract the parameter if it supposed to be a string or a copy.

If you just want the API to have the appearance of a single constructor, but still want type safety, you can use the techniques above to create your actual constructor implementation, and use inline functions and preprocessor tricks to give the appearance of a single constructor, with type safety.

String new_StringImpl(enum StringCtor type, const void *arg);

static inline String new_StringImplDefault () {
    return new_StringImpl(SC_DEFAULT, 0);
}
static inline String new_StringImplCstr (const char *s) {
    return new_StringImpl(SC_C_STR, s);
}
static inline String new_StringImplCopy (String *s) {
    return new_StringImpl(SC_COPY, s);
}

#define new_String_Paste(TYPE) new_String_ ## TYPE
#define new_String_SC_DEFAULT(ARG) new_StringImplDefault()
#define new_String_SC_C_STR(ARG) new_StringImplCstr(ARG)
#define new_String_SC_COPY(ARG) new_StringImplCopy(ARG)

#define new_String(TYPE, ...) new_String_Paste(TYPE)(__VA_ARGS__)

String s1 = new_String(SC_DEFAULT);
String s2 = new_String(SC_C_STR, "hello");
String s3 = new_String(SC_COPY, &s2);

Notice with the variadic macro, SC_DEFAULT does not need a second parameter any more. At sufficient optimization levels, the code translates to just a call to the single implementation function, with the benefit of compile time type safety checks. So at the cost of some more coding on your part, you can give the user of your library the appearance of a single constructor API with all the type safety of multiple constructor functions.


There is no standard convention but in case of many overloads of a more or less similar function, people usually refer to OpenGL function naming convention.

The method provided by @user315052 is typically not preferred because of type safety issues (error prone, hard to change etc) and/or inconvenience when you have more than one argument to pass. But it is sometimes used though for different technical reasons (kernel system calls is a good example).


I don't think that there is a well-established convention on how to handle overloading. Your second approach is fine; use common sense to keep the names reasonably short but still easily understandable.

Some systematic naming convention like your first approach (or as in OpenGL) might be a good idea in some cases, but it makes the code a bit less self-explanatory; anyone reading your code would need to understand your naming convention first.


Modern C, AKA C11, can do more than you think. It has type generic macros that can accomplish something similar to function overloading in C++ with the new _Generic expressions.

As internal interfaces for your library you still would need the function declarations:

String new_String_from_cstr(const char *str);
String new_String_copy(String s);

Now your type generic macro could look like:

#define new_String(X)                       \
_Generic((X),                               \
         const char*: new_String_from_cstr, \
         String: new_String_copy)(X)

C11 is not yet completely implemented, but I think the most recent clang compiler already has what you need here.

For other compilers (gcc and cousins) you may have a look into P99, that emulates features that come close to that. P99 also has tricks to deal with macros that receive different numbers of parameters.

If you use such tools as user interfaces, the naming convention for the functions themselves becomes much less important.


Need Your Help

matching first child from a node

xml xslt

I've the below XML code.