2

I want to change all my int type in my program to support arbitrary position integer. I chose to use GMP.

I am thinking about is it possible to do a #define to replace all int to mpz_class.

I start by a small program

#include <iostream>
#define int long long int
using namespace std;

int main(){
    // .... code
}

The compiler is already complaining about main have to return an int type.

Is it possible to add exception to #define? or this is a really bad idea to do so?

Timothy Leung
  • 1,407
  • 7
  • 22
  • 39
  • That sounds really dangerous. – OldProgrammer Jan 06 '14 at 03:46
  • 2
    Do not do this. Use a `typedef` and change the `ints` to use this instead. Otherwise all sorts of things (header files etc, linker problems) will fail. – Ed Heal Jan 06 '14 at 03:47
  • 1
    This is undefined behaviour AFAIK. I believe the wording regarding this is "ill-formed". Eh, guess not, but *A translation unit shall not #define or #undef names lexically identical to keywords,* ***to the identifiers listed in Table 3,*** *or to the attribute-tokens described in 7.6.* – chris Jan 06 '14 at 03:47
  • 1
    You don't necessarily want to change *all* "int" to GMP "Big Integers". And you should absolutely *not* "alias" int with a global #define. Your best bet is manual search-and-replace (IMHO). ADDED BENEFIT: you'll probably come up with ideas for refactoring your code to localize the use of your "Big Integers"... – FoggyDay Jan 06 '14 at 03:48
  • what are u trying to do with this statement #define int long long int – Nik Jan 06 '14 at 03:49
  • @chris: Something cannot be both ill-formed and undefined. – Lightness Races in Orbit Jan 06 '14 at 03:51
  • @LightnessRacesinOrbit, Yeah, that wasn't pro thinking on my part. – chris Jan 06 '14 at 03:51
  • Redefining such a basic type will break any API using that type, including the API for `main` as you've discovered. Don't do it. Editors generally have global find/replace, and you can go back and fix the errors that you introduce. – Mark Ransom Jan 06 '14 at 03:53
  • so it is better to do it manually and chaning them one by one? I am afraid I will miss some of them if I do it in this way – Timothy Leung Jan 06 '14 at 03:55
  • 1
    @TimothyLeung: You're stuck doing that, because you didn't think ahead and design with `typedef`s! – Lightness Races in Orbit Jan 06 '14 at 03:55
  • So the proper way to do it is to `typedef` a `type` to a generic word? So next time I can simply change the typedef? – Timothy Leung Jan 06 '14 at 03:57
  • @TimothyLeung - Yes. Also is more descriptive of the return/parameter type – Ed Heal Jan 06 '14 at 03:59
  • @TimothyLeung: Yeah, I mean, don't go over the top with it or anything, but this seems like it would have been a fairly decent candidate for such treatment. – Lightness Races in Orbit Jan 06 '14 at 03:59
  • 2
    Next you'll be all `#define private public` and it'll be dogs and cats living together, mass hysteria! – Retired Ninja Jan 06 '14 at 04:08
  • possible duplicate of [C++ Data Types vs. C# Data Types](http://stackoverflow.com/questions/10476353/c-data-types-vs-c-sharp-data-types) – Ben Voigt Jan 06 '14 at 04:11

3 Answers3

4

Redefining a keyword is prohibited iff you include any standard headers. Here, you included <iostream> so your program is ill-formed.

Otherwise, knock yourself out! Wait, no, don't, because this would still be really silly.

Instead, refactor your code to use some new type called, say, my_integer (but with a much better name):

typedef int my_integer;

Then, when you want to change from int to mpz_class, you just change the definition of my_integer:

typedef mpz_class my_integer;
Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

use main without int like this:

#include <iostream>
#define int long long int
using namespace std;

main(){
    // .... code
}
somag
  • 11
  • 1
0

The simple answer: although technically possible you are not allowed to #define any of the reserved identifiers.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380