Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function overloading in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int addi(int a, int b) {
return a + b;
}
char *adds(char *a, char *b) {
char *res = malloc(strlen(a) + strlen(b) + 1);
strcpy(res, a);
strcat(res, b);
return res;
}
#define add(a, b) _Generic(a, int: addi, char*: adds)(a, b)
int main(void) {
int a = 1, b = 2;
printf("%d\n", add(a, b)); // 3
char *c = "hello ", *d = "world";
printf("%s\n", add(c, d)); // hello world
return 0;
}
@jasiek

This comment has been minimized.

Show comment
Hide comment
@jasiek

jasiek Jul 26, 2015

I wonder if this'll work on the Arduino.

jasiek commented Jul 26, 2015

I wonder if this'll work on the Arduino.

@dataf3l

This comment has been minimized.

Show comment
Hide comment
@dataf3l

dataf3l Jul 26, 2015

works as advertised on osx.

dataf3l commented Jul 26, 2015

works as advertised on osx.

@Tyler-Hardin

This comment has been minimized.

Show comment
Hide comment
@Tyler-Hardin

Tyler-Hardin Jul 26, 2015

@jasiek, if you can find a C11 compiler for Arduino. GCC supports the _Generic portion of C11 since 4.9, so it should work with avr-gcc-4.9 or newer.

Tyler-Hardin commented Jul 26, 2015

@jasiek, if you can find a C11 compiler for Arduino. GCC supports the _Generic portion of C11 since 4.9, so it should work with avr-gcc-4.9 or newer.

@2mac

This comment has been minimized.

Show comment
Hide comment
@2mac

2mac Jul 26, 2015

You create a memory leak by not storing a pointer to the concatenated string before printing it, since now you can't free it after you malloc'd it.

2mac commented Jul 26, 2015

You create a memory leak by not storing a pointer to the concatenated string before printing it, since now you can't free it after you malloc'd it.

@patrickt

This comment has been minimized.

Show comment
Hide comment
@patrickt

patrickt Jul 26, 2015

clang provides __attribute__((overloadable)), which enables a limited version of C++-style name-mangling based polymorphism, so that you don't have to write a _Generic wrapper for each function.

patrickt commented Jul 26, 2015

clang provides __attribute__((overloadable)), which enables a limited version of C++-style name-mangling based polymorphism, so that you don't have to write a _Generic wrapper for each function.

@lpereira

This comment has been minimized.

Show comment
Hide comment
@lpereira

lpereira Jul 26, 2015

Here's another example I wrote the other day, that's actually useful: number parsing, regardless of its type: parse_number().

lpereira commented Jul 26, 2015

Here's another example I wrote the other day, that's actually useful: number parsing, regardless of its type: parse_number().

@KLuka

This comment has been minimized.

Show comment
Hide comment
@KLuka

KLuka Jul 26, 2015

@2mac memory is freed at line 25 😄

OT: thanks for the interesting stuff...

KLuka commented Jul 26, 2015

@2mac memory is freed at line 25 😄

OT: thanks for the interesting stuff...

@daurnimator

This comment has been minimized.

Show comment
Hide comment
@daurnimator

daurnimator Jul 26, 2015

Without C11:

#define NARG_(_15, _14, _13, _12, _11, _10, _9, _8, _7, _6, _5, _4, _3, _2, _1, N, ...) N
#define NARG(...) NARG_(__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define PASTE(a, b) a ## b
#define XPASTE(a, b) PASTE(a, b)

int foo(int a, int b) {
    return 1;
}
#define foo2(a,b) foo((a), (b))
#define foo1(x) foo2(x,0)
#define foo(...) XPASTE(foo, NARG(__VA_ARGS__))(__VA_ARGS__)

daurnimator commented Jul 26, 2015

Without C11:

#define NARG_(_15, _14, _13, _12, _11, _10, _9, _8, _7, _6, _5, _4, _3, _2, _1, N, ...) N
#define NARG(...) NARG_(__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define PASTE(a, b) a ## b
#define XPASTE(a, b) PASTE(a, b)

int foo(int a, int b) {
    return 1;
}
#define foo2(a,b) foo((a), (b))
#define foo1(x) foo2(x,0)
#define foo(...) XPASTE(foo, NARG(__VA_ARGS__))(__VA_ARGS__)
@theonewolf

This comment has been minimized.

Show comment
Hide comment
@theonewolf

theonewolf Jul 26, 2015

Is there a way to do stricter type checking on more than one argument? As I read this, it is only checking the type of the first argument?

theonewolf commented Jul 26, 2015

Is there a way to do stricter type checking on more than one argument? As I read this, it is only checking the type of the first argument?

@dubslow

This comment has been minimized.

Show comment
Hide comment
@dubslow

dubslow Jul 26, 2015

  1. Type checking only on one argument

  2. main leaks memory

dubslow commented Jul 26, 2015

  1. Type checking only on one argument

  2. main leaks memory

@Benabik

This comment has been minimized.

Show comment
Hide comment
@Benabik

Benabik Jul 26, 2015

@jasiek: The Arduino IDE uses C++, so function overloading works just fine. (So do templates, but you have to define them in a .h file rather than the default extension-less file.)

Benabik commented Jul 26, 2015

@jasiek: The Arduino IDE uses C++, so function overloading works just fine. (So do templates, but you have to define them in a .h file rather than the default extension-less file.)

@danetrata

This comment has been minimized.

Show comment
Hide comment
@danetrata

danetrata Jul 26, 2015

@KLuka
It's not good practice to not free something you've malloc'd.

danetrata commented Jul 26, 2015

@KLuka
It's not good practice to not free something you've malloc'd.

@dbevzenko

This comment has been minimized.

Show comment
Hide comment
@dbevzenko

dbevzenko Jul 26, 2015

Still not supported by Intel C compiler though.

dbevzenko commented Jul 26, 2015

Still not supported by Intel C compiler though.

@jaytaylor

This comment has been minimized.

Show comment
Hide comment
@Fusion

This comment has been minimized.

Show comment
Hide comment
@Fusion

Fusion Jul 26, 2015

@dubslow Yes, main leaks memory. I realize now that github has its own "grammar police" since that was not the point of this gist.

Fusion commented Jul 26, 2015

@dubslow Yes, main leaks memory. I realize now that github has its own "grammar police" since that was not the point of this gist.

@polkovnikov-ph

This comment has been minimized.

Show comment
Hide comment
@polkovnikov-ph

polkovnikov-ph Jul 26, 2015

@Fusion I'd say it's not github, but C community, as memory leaks is something to avoid even in examples that are this simple.

polkovnikov-ph commented Jul 26, 2015

@Fusion I'd say it's not github, but C community, as memory leaks is something to avoid even in examples that are this simple.

@spekode

This comment has been minimized.

Show comment
Hide comment
@spekode

spekode Jul 26, 2015

DAE notice the memory leak???????????????

spekode commented Jul 26, 2015

DAE notice the memory leak???????????????

@tkellogg

This comment has been minimized.

Show comment
Hide comment
@tkellogg

tkellogg Jul 27, 2015

OMG did you guys see the memory leak?!!

tkellogg commented Jul 27, 2015

OMG did you guys see the memory leak?!!

@Ygrex

This comment has been minimized.

Show comment
Hide comment
@Ygrex

Ygrex Aug 19, 2015

'a' gets evaluated twice

Ygrex commented Aug 19, 2015

'a' gets evaluated twice

@lrocha3

This comment has been minimized.

Show comment
Hide comment
@lrocha3

lrocha3 Feb 16, 2017

There is a memory leak. You allocate with malloc but you never free the allocated memory. You should fix that.

lrocha3 commented Feb 16, 2017

There is a memory leak. You allocate with malloc but you never free the allocated memory. You should fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment