views:

72

answers:

4

I implemented a function called abs(). I get this error:

Intrinsic function, cannot be defined

What have I done wrong? I'm using Visual Studio 2005.

+2  A: 

The problem is not being in a header or not.

The problem is that intrinsic functions, i.e., functions that the compiler recognizes and implements itself, generally with optimizations that wouldn't be available in C code alone, cannot be defined.

Artefacto
Thanks. Could you give an example? What do you mean by "compiler... implements itself"? What if I need to use that abs() function?
snakile
@sna `#include <stdlib.h>`
Artefacto
@Artefacto, I include <stdlib.h>. Still doesn't work
snakile
@sna You mean you want to use an abs function *different* from the one in the standard c library?... just name it something else...
Artefacto
Oh, I see. Thank you.
snakile
A: 

The names of all mathematical functions (see math.h)

The names of all mathematical functions prefixed by 'f' or 'l'.

Are reserved for the implementation.

Martin York
+1  A: 

Intrinsic function, cannot be defined

In this case, intrinsic means that the compiler already has an implementation of a function called abs, and which you cannot redefine.

Solution? Change your function's name to something else, snakile_abs for example.

Check the MSDN documentation on the abs function for more information.

mctylr
A: 

Defining static int abs(int x) { ... } should be legal, but simply int abs(int x) { ... } has undefined behavior, and thus one reasonable thing a compile could do is issue an error.

R..
Some header may still have `#define abs __builtin_magic_abs` orthe like. Since the preprocessor sees the text first, you still end up trying to define `static int __buildin_magic_abs(int x){...}`. Since abs() is a name defined in the C standard library, it is likely to be unwise (and certainly not portable) to attempt to replace it by name.
RBerteig
As long as you don't `#include` any header that defines `abs` or `#undef` it before defining your own version, the Standard specifically allows you to replace it with a `static` function. Replacing the `extern` version is undefined behavior, however.
R..