用实际代码替换定义语句

发布于 2025-02-03 18:19:02 字数 1272 浏览 3 评论 0原文

我知道#define并不是很好的使用,我不确定这是否是重复的,但是我找不到最好的方法:

我有一个使用定义的程序像:

#define True GetObject(true)

我需要用实际的代码替换定义语句

,但我想不出一种方法来制作以下代码:

int main() {

  auto c = True;

  return 0;
}

在编译时间转变为此:

int main() {

  auto c = GetObject(true);

  return 0;
}

摘要: 我想要确切的替换为“定义”为代码,我找到了类似内联函数的东西 可以提供帮助,但是有没有办法进行内联变量?

我尝试了以下操作,但最终出现了一个错误

inline Object True = GetObject(true);

注意 :我无法使object/getObject成为constexpr class

注意2 < < /em>:我想避免将true转换为true()如果可能的话,

这基本上是用于教育目的的问题,但我想在我正在写的小图书馆,如果您能告诉我最好的方法是什么,我会很高兴

!!!

编辑1

,上面的第一个尚不清楚,我希望true true true getObject(true)函数每次

返回值 时将是相同的,但是函数调用是必需的

编辑2

我认为没有必要解释这一点,但是,我创建的库是一个简单的层(这对此并不重要),

宏名称true是完全随机的,它可以被命名为完全不同的东西(我只是将其用于测试)。

宏用于创建我需要的类要在我的代码中使用很多,我还需要它来创建类的新实例(不仅是副本),

我还需要大量更新类,以便在构造函数中添加更多常数,我需要拥有一些简单的 替换每个实例都不好

做到的方法,我认为进入我的10个标题/源中的每个源并用代表“ True”和其他状态的值

。关于删除()的部分是因为我认为在看起来像变量(或某种编译时常数)中看到很多括号很方便吗?)

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?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽你眉间 2025-02-10 18:19:02

以下原则上可能与您的代码相似。
它用#define求解,并将serialno设置为每个object,并将某些内容打印到控制台上:

#define True GetObject(true)
#define False GetObject(false)

#include <iostream>
using namespace std;

class Object {
public:
    Object(bool b, int serial) : _b(b), _serialno(serial) {};

    bool _b;
    int _serialno;
};

Object GetObject(bool b) {
    static int curserial = 0;

    Object result(b, ++curserial);

    cout << "Created Object with serialno " << result._serialno << " and value " << boolalpha << result._b << endl;
    return result;
}

int main() {
    auto c = True;
    auto d = True;
    auto e = False;

    return 0;
}

它生成了输出

Created Object with serialno 1 and value true
Created Object with serialno 2 and value true
Created Object with serialno 3 and value false

,我们将其更改它对于没有“ #define”的相同结果:

#include <iostream>
using namespace std;

class Object;
void GotObject(Object& o);

class Object {
public:
    Object(bool b) : _b(b), _serialno(++curserial) {};
    Object(const Object& other) : _b(other._b), _serialno(++curserial) {
        GotObject(*this);
    };

    bool _b;
    int _serialno;

    inline static int curserial = 0;
};

void GotObject(Object& o) {
    cout << "Created Object with serialno " << o._serialno << " and value " << boolalpha << o._b << endl;
}

inline Object True(true);
inline Object False(false);

int main() {
    auto c = True;
    auto d = True;
    auto e = False;

    return 0;
}

输出:

Created Object with serialno 3 and value true
Created Object with serialno 4 and value true
Created Object with serialno 5 and value false

每次我们为新变量分配true true false 的值在getObject中做到了。

小型变体:如果我们选择此替代自定义构造函数,而不是代码中的构造函数,

    Object(bool b) : _b(b), _serialno(0) {};

我们将获得输出:

Created Object with serialno 1 and value true
Created Object with serialno 2 and value true
Created Object with serialno 3 and value false

区别是,是否也计算出serialnotrue> truefalse本身或仅在将它们分配给变量之后。

为了生成truefalse,第一个构造函数被调用,以进行以下分配到其他变量,即第二个构造函数。

您还可以保留bool _original actibal object 仅调用getObject(),它指出您是否从原始true复制false。仅对于truefalse才是正确的。您可以通过他们称呼特殊构造函数来识别那些。如果您想安全使用安全,则可以将该构造函数私有化,因此只能由朋友函数或静态出厂方法调用。

在以下代码中,从cf的分配中未调用gotObject。

#include <iostream>
using namespace std;

class Object;
void GotObject(Object& o);

class Object {
public:
    Object(bool b) : _b(b), _serialno(0), _original(true) {};
    Object(const Object& other) : _b(other._b), _original(false), _serialno(0) {
        if (other._original)
            GotObject(*this);
    };

    bool _original;
    bool _b;
    int _serialno;
};

void GotObject(Object& o) {
    static int curserial = 0;
    o._serialno = ++curserial;

    cout << "Created Object with serialno " << o._serialno << " and value " << boolalpha << o._b << endl;
}

inline Object True(true);
inline Object False(false);

int main() {
    auto c = True;
    auto d = True;
    auto e = False;
    auto f = c;

    return 0;
}

输出:

Created Object with serialno 1 and value true
Created Object with serialno 2 and value true
Created Object with serialno 3 and value false

The following is probably similar in principle to your code.
It is solved with #define and sets a serialno to each Object and prints something out to the console:

#define True GetObject(true)
#define False GetObject(false)

#include <iostream>
using namespace std;

class Object {
public:
    Object(bool b, int serial) : _b(b), _serialno(serial) {};

    bool _b;
    int _serialno;
};

Object GetObject(bool b) {
    static int curserial = 0;

    Object result(b, ++curserial);

    cout << "Created Object with serialno " << result._serialno << " and value " << boolalpha << result._b << endl;
    return result;
}

int main() {
    auto c = True;
    auto d = True;
    auto e = False;

    return 0;
}

which generates the output

Created Object with serialno 1 and value true
Created Object with serialno 2 and value true
Created Object with serialno 3 and value false

Now we change it to the same result without '#define':

#include <iostream>
using namespace std;

class Object;
void GotObject(Object& o);

class Object {
public:
    Object(bool b) : _b(b), _serialno(++curserial) {};
    Object(const Object& other) : _b(other._b), _serialno(++curserial) {
        GotObject(*this);
    };

    bool _b;
    int _serialno;

    inline static int curserial = 0;
};

void GotObject(Object& o) {
    cout << "Created Object with serialno " << o._serialno << " and value " << boolalpha << o._b << endl;
}

inline Object True(true);
inline Object False(false);

int main() {
    auto c = True;
    auto d = True;
    auto e = False;

    return 0;
}

Output:

Created Object with serialno 3 and value true
Created Object with serialno 4 and value true
Created Object with serialno 5 and value false

Each time we assign the values of True or False to new variables the copy constructor is called and can do, whatever you did in GetObject.

Small variant: If we choose this alternative custom constructor instead of the one in the code,

    Object(bool b) : _b(b), _serialno(0) {};

we would get as output:

Created Object with serialno 1 and value true
Created Object with serialno 2 and value true
Created Object with serialno 3 and value false

The difference is, whether the serialno is also counted up for True and False themselves or only after assigning those to a variable.

For Generating True and False the first constructor is called, for the following assignments to other variables, the second constructor.

You could also keep a bool _original variable inside Object to only call GetObject(), which states whether you copy from the original True or False. It is true only for True and False. You can recognize those by them calling a special constructor. If you want to make it safe to use, you can make that constructor private, so it can only be called by friend functions or by static factory methods.

In the following code, GotObject is not called from assigning from c to f.

#include <iostream>
using namespace std;

class Object;
void GotObject(Object& o);

class Object {
public:
    Object(bool b) : _b(b), _serialno(0), _original(true) {};
    Object(const Object& other) : _b(other._b), _original(false), _serialno(0) {
        if (other._original)
            GotObject(*this);
    };

    bool _original;
    bool _b;
    int _serialno;
};

void GotObject(Object& o) {
    static int curserial = 0;
    o._serialno = ++curserial;

    cout << "Created Object with serialno " << o._serialno << " and value " << boolalpha << o._b << endl;
}

inline Object True(true);
inline Object False(false);

int main() {
    auto c = True;
    auto d = True;
    auto e = False;
    auto f = c;

    return 0;
}

Output:

Created Object with serialno 1 and value true
Created Object with serialno 2 and value true
Created Object with serialno 3 and value false
黯然 2025-02-10 18:19:02

但是有没有办法进行内联变量?

是的,由于C ++ 17可以定义内联变量。内联变量与其他名称空间范围变量相同,只是您可以在标题中定义它们,该标题可能包含在多个翻译单元中。例子:

inline auto True = GetObject(true);

我希望将“定义”的确切替换为代码

变量不会以与宏替换完全相同的方式行为。 getObject函数将仅调用一次来初始化变量,而不是每次宏扩展到函数调用时。

如果要有函数调用,那么理想的解决方案是用属于语法的括号明确编写函数调用。

注2:如果可能的话,我想避免转到true()

如果您想进行函数调用,这是一件坏事。我建议放弃您的欲望之一。

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.

山田美奈子 2025-02-10 18:19:02

我希望true每次呼叫 getObject(true)函数

,然后true()是您没有宏的最好的。

另外,如果您要在结果实例上调用方法,请使其免费功能,并让它们为自己创建一个实例:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文