Operator overloading in C
Asked Answered
B

7

44

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.

Biota answered 5/8, 2010 at 17:26 Comment(5)
Just don't :). Whats wrong with struct RGB {int8_t red, green, blue;};. Do you really need all that meta, all the damn time?Oviparous
Even if C would support operator overloading: does operator* and operator/ make sense on colors?Molal
@Doc Brown- Why wouldn't it make sense? Once the operators are overloaded they could mean anything, not just 'multiply' and 'divide'.Dibasic
@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.Molal
"does operator* and operator/ make sense on colors?" Alpha (pre)multiplication, perhaps?Voice
Y
95

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

Yun answered 5/8, 2010 at 17:29 Comment(8)
Because you don't have references in C!Allegro
@Allegro 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.Hispanic
@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.Tanyatanzania
@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.Hispanic
@YoYoYonnY: You're not just moving the goalposts, you're raising them above your head then flailing them wildly around in the air.Tanyatanzania
@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.Hispanic
@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.Tanyatanzania
@Hispanic everything you've listed there is true of C in relation to assembly. It is just syntactic sugar. However some C++ functionality cannot be emulated by C, (for example combining CRTP and SFINAE) meaning it is not merely syntactic. The reverse is not the case.Lombok
B
32

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.

Bygone answered 14/3, 2013 at 22:10 Comment(3)
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.Bygone
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.Bygone
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.Bygone
E
27

There is no operator overloading in C.

Expectorant answered 5/8, 2010 at 17:29 Comment(1)
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.Yun
H
18

You cannot overload these operators in C.

Hollyhock answered 5/8, 2010 at 17:28 Comment(0)
E
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.

Electric answered 5/8, 2010 at 17:30 Comment(2)
Writing a C library in C++ would be quite funny. Including the header file gives instant errors :)Biota
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...Putupon
D
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
}
Dibasic answered 5/8, 2010 at 17:32 Comment(0)
E
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!

Esau answered 21/10, 2015 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.