43

I am trying to overload some operators:

/* Typedef is required for operators */
typedef int Colour;

/* Operators */
Colour operator+(Colour colour1, Colour colour2);
Colour operator-(Colour colour1, Colour colour2);
Colour operator*(Colour colour1, Colour colour2);
Colour operator/(Colour colour1, Colour colour2);

I get this error for each tried overloading:

expected '=', ',', ';', 'asm' or '__attribute__' before '+' token

I can't find any good documentation on operator overloading. Googling results in C++ tutorials which use classes. In C there are no classes. Can anyone help me? Thanks.

  • 1
    Just don't :). Whats wrong with `struct RGB {int8_t red, green, blue;};`. Do you really need all that meta, all the damn time? – Matt Joiner Aug 05 '10 at 17:31
  • 5
    Even if C would support operator overloading: does operator* and operator/ make sense on colors? – Doc Brown Aug 05 '10 at 17:35
  • 3
    @Doc Brown- Why wouldn't it make sense? Once the operators are overloaded they could mean anything, not just 'multiply' and 'divide'. – bta Aug 05 '10 at 19:44
  • 1
    @bta: agreed, but when they don't mean multiply and divide, why bothering with overloading at all? Even in C++, it is very bad style to use operator overloading when the operators don't match their original meanings. – Doc Brown Aug 05 '10 at 19:59
  • 2
    "does operator* and operator/ make sense on colors?" Alpha (pre)multiplication, perhaps? – Brian S Aug 05 '10 at 20:15

7 Answers7

90

C does not support operator overloading (beyond what it built into the language).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    Because you don't have references in C! – Ali May 26 '15 at 03:21
  • 10
    @Ali References are simply syntactical sugar. Same goes for other C++ features like const, overloading, namespaces and classes. They don't add any functionality, they simply add other ways to do the same thing. This doesn't mean they are redundant, but it also definitly doesn't mean C++ features cannot be implemented in C. Everything C++ can do, C can do too, just with different syntax (They call it 'syntactical sugar' for a reason). In this particulair case replacing references with pointers would work perfectly fine. – yyny May 15 '16 at 00:04
  • 4
    @YoYoYonnY: By that logic, the _entire_ C language, and the _entire_ C++ language, are just "syntactic sugar" for the facilities provided by your computer's instruction set. – Lightness Races in Orbit Feb 14 '18 at 16:44
  • 5
    @LightnessRacesinOrbit No, because "syntactic sugar" is "A language construct designed to make code easier to read or express, without affecting functionality or expressive power". You can't express structs, or arrays, or references in Assembly (Some assemblers have extensions). You _can_ express references in C, you just have to use reference and dereference syntax. – yyny Feb 14 '18 at 22:11
  • 1
    @YoYoYonnY: You're not just moving the goalposts, you're raising them above your head then flailing them wildly around in the air. – Lightness Races in Orbit Feb 14 '18 at 22:20
  • @LightnessRacesinOrbit I didn't even write that comment to argue that references are syntactic sugar (Although I do believe they are), I was trying to make a point that C could have operator overloading without any of the features that C++ uses (and does not need) to implement them. Also, the definition for syntactic sugar is well established, I didn't pull it out of thin air if that's what you're suggesting. – yyny Feb 15 '18 at 01:20
  • 2
    @YoYoYonnY: I guess I'm disputing your interpretation of the definition for syntactic sugar which appears to encompass more or less the entire C++ language. I'm basing that on the would-be transitive result of such an interpretation (the whole universe is syntactic sugar). I would certainly not call `const` "syntactic sugar". But I confess I haven't read the strict definition in a while. – Lightness Races in Orbit Feb 15 '18 at 01:27
30

You need a time machine to take you back to 1985, so that you may use the program CFront. It appears that 'C' use to support operator overloading; to the sophisticated enough it still can. See Inside the C++ Object Model by Stanley B. Lippman. OMG, C++ was C! Such a thing still exists.

This answer confirms the others. 'C' by itself does not directly support overloading. However, the important point is a programmer can write code that understands code. You need a tool that transforms source to implement this. In this case, such tools already exist.

A paper, Meta-Compilation for C++, 2001 by Edward D. Willink has interesting examples of design functionality, where extending a language is useful. The combination of *nix shell script and make rules often allow such transformation. Other examples are Qt MOC, the tools Lex and Yacc, halide etc. So while 'C' itself doesn't accommodate this directly, it does if you build host tools.

In this particular example the overloading may not make sense. However, it could make a lot of sense for a program needing arbitrary precision math.

artless noise
  • 21,212
  • 6
  • 68
  • 105
  • Perhaps importantly, the operator overloading can be supported by 'translating C++ syntax' to a 'C' equivalent that can be compiled in a straight-forward manner. Not all C++ language features are easy to translate and require linker and other runtime compiler support. For example, templates and features new to C++11 and later. – artless noise Aug 19 '21 at 17:39
  • `red/black != blue*pink`... not really sure about the use cases of the original post. Also, you have different type of 'color addition'. Pigments, dyes, are different than light sources (probably the OPs concept) as it relates to current computer display technology. – artless noise Nov 15 '21 at 15:00
  • Another more modern tact is to use a compiler plug-in which gcc started to support in earnest around 2011 and at this point in time they are a very good technical path to augment a language (as Halide has done compared to the other examples given) for domain specific structural solutions to help manage complexity and ensure correctness. This avoids the parsing issue explored in the FOG thesis paper (which is still a good read this day imho). Clang support plugins for a longer period, but only recently has code generation capabilities on par with gcc. – artless noise May 27 '22 at 13:28
27

There is no operator overloading in C.

Justin Ardini
  • 9,768
  • 2
  • 39
  • 46
  • 23
    Of course there is -- just for example, `-`, `+`, `/`, `*`, all apply equally well to `int` or `double`. What it doesn't support is adding any overloads beyond what's built in. – Jerry Coffin Aug 05 '10 at 18:17
18

You cannot overload these operators in C.

Pierre-Antoine LaFayette
  • 24,222
  • 8
  • 54
  • 58
13

C does not support operator overloading at all.

You can only implement operations as functions:

Colour colour_add(Colour c1, Colour c2);
Colour colour_substract(Colour c1, Colour c2);
...

You could also switch to C++, but it may be overkill to do it just for the overloading.

Roberto Bonvallet
  • 31,943
  • 5
  • 40
  • 57
  • 2
    Writing a C library in C++ would be quite funny. Including the header file gives instant errors :) –  Aug 07 '10 at 14:43
  • Actually, that's a rather easy task - you just have to do it the right way (all this #ifdef __cplusplus \ extern "C" { \ #endif stuff...). Of course, within the extern "C" block, you can only provide what C allows, which means, a. o., no operator overloading again... – Aconcagua Feb 05 '16 at 10:22
8

Operator overloading is not available in C. Instead, you will have to use a function to "pseudo-overload" the operators:

Colour add_colours(Colour c1, Colour c2) {
    return c1 + c2; // or whatever you need to do
}
bta
  • 43,959
  • 6
  • 69
  • 99
3

If you want comparable concision, the use of macros is the best available alternative:

void Type_getSomething(int id); //or some other complex series of instructions

#define g(id) Type_getSomething(id)

...it's such a pity that the use of square brackets isn't possible for macros!

Engineer
  • 8,529
  • 7
  • 65
  • 105