用实际代码替换定义语句
我知道#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下原则上可能与您的代码相似。
它用
#define
求解,并将serialno
设置为每个object
,并将某些内容打印到控制台上:它生成了输出
,我们将其更改它对于没有“ #define”的相同结果:
输出:
每次我们为新变量分配
true
true false 的值在getObject
中做到了。小型变体:如果我们选择此替代自定义构造函数,而不是代码中的构造函数,
我们将获得输出:
区别是,是否也计算出
serialno
的true> true
和false
本身或仅在将它们分配给变量之后。为了生成
true
和false
,第一个构造函数被调用,以进行以下分配到其他变量,即第二个构造函数。您还可以保留
bool _original
actibal object 仅调用getObject()
,它指出您是否从原始true复制
或false
。仅对于true
和false
才是正确的。您可以通过他们称呼特殊构造函数来识别那些。如果您想安全使用安全,则可以将该构造函数私有化,因此只能由朋友函数或静态出厂方法调用。在以下代码中,从
c
到f
的分配中未调用gotObject。输出:
The following is probably similar in principle to your code.
It is solved with
#define
and sets aserialno
to eachObject
and prints something out to the console:which generates the output
Now we change it to the same result without '#define':
Output:
Each time we assign the values of
True
orFalse
to new variables the copy constructor is called and can do, whatever you did inGetObject
.Small variant: If we choose this alternative custom constructor instead of the one in the code,
we would get as output:
The difference is, whether the
serialno
is also counted up forTrue
andFalse
themselves or only after assigning those to a variable.For Generating
True
andFalse
the first constructor is called, for the following assignments to other variables, the second constructor.You could also keep a
bool _original
variable insideObject
to only callGetObject()
, which states whether you copy from the originalTrue
orFalse
. It is true only forTrue
andFalse
. 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
tof
.Output:
是的,由于C ++ 17可以定义内联变量。内联变量与其他名称空间范围变量相同,只是您可以在标题中定义它们,该标题可能包含在多个翻译单元中。例子:
变量不会以与宏替换完全相同的方式行为。
getObject
函数将仅调用一次来初始化变量,而不是每次宏扩展到函数调用时。如果要有函数调用,那么理想的解决方案是用属于语法的括号明确编写函数调用。
如果您想进行函数调用,这是一件坏事。我建议放弃您的欲望之一。
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:
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.
This is a bad thing to want if you want to make a function call. I recommend giving up one of your desires.
,然后
true()
是您没有宏的最好的。另外,如果您要在结果实例上调用方法,请使其免费功能,并让它们为自己创建一个实例:
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: