Home > Blockchain >  Replacing define statement with actual code
Replacing define statement with actual code

Time:06-03

I know that #define is not really good to use, I am not sure if that's a duplicate but I couldn't find what's the best way is to do this:

I have a program that uses a definition like:

#define True GetObject(true)

I need to replace the define statement with actual code

but I can't think of a way to make it so the following code:

int main() {

  auto c = True;

  return 0;
}

turns into this at compile time:

int main() {

  auto c = GetObject(true);

  return 0;
}

Summary: I want an exact replacement of "define" as code, I found something like an inline function could help, but is there a way to make an inline variable?

I tried the following but ended up with an error

inline Object True = GetObject(true);

NOTE: I can't make Object/GetObject a constexpr class

NOTE 2: I'd like to avoid turning True to True() if that's possible

This is basically a question for educational purposes but I would like to use it in a small library I am writing, If you could tell me what would be the best way to do this I'd be really happy

Thanks!!!

EDIT 1

As the first above is not quite clear, I'd like True to call GetObject(true) function every time

The returned value is going to be the same but the function call is necessary

EDIT 2

I didn't think it is necessary to explain this but, the library I am creating is a simple layer (that's not really important for this),

The macro name True is completely random, it could be named something completely different (I am just using it for testing)

The macro is used to create a Class that I need to use a lot in my code and I also need it to create a new instance of the class (not just a copy)

I also need to update the class a lot so to add more constants in the constructor I would need to have some simple way to do, I don't think it would be good to go in each of my 10 headers/sources and replace every instance

with the values that represent 'True' and other states.

the part about removing () is because I don't think it's convenient to see a lot of parenthesis in something that looks like a variable (or maybe some kind of compile-time constant?)

CodePudding user response:

but is there a way to make an inline variable?

Yes, since C 17 it's possible to define inline variables. Inline variables are same as other namespace scope variables, except you can define them in a header that may be included into more than one translation unit. Example:

inline auto True = GetObject(true);

I want an exact replacement of "define" as code

A variable won't behave in the exactly same way as the macro replacement. The GetObject function will be called only once to initialise the variable, as opposed to every time when the macro expands to a function call.

If you want to have a function call, then the ideal solution is to explicitly write a function call, with the parentheses that are part of the syntax.

NOTE 2: I'd like to avoid turning True to True() if that's possible

This is a bad thing to want if you want to make a function call. I recommend giving up one of your desires.

CodePudding user response:

I'd like True to call GetObject(true) function every time

Then True() is the best you can get without macros.

Alternatively, if you're going to be calling methods on the resulting instance, make them free functions and let them create an instance for themselves:

namespace True
{
    void foo()
    {
        TrueObj obj(true);
        // ...
    }
}
True::foo();
  • Related