在编译时修改函数 [英] modify function during compiletime

查看:153
本文介绍了在编译时修改函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解决了一个简单的问题:
我想要一个被多次调用的功能,但是第一次被调用时,它应该做一些额外的事情.

我知道这不是工作,但您可能会更好地了解我想要的东西:

i got that simple problem to be solved:
i want a funktion that is called several times, but the first time it is called it should do something additional.

I know that aint work but you might get a better idea of what i want:

#define DOIT
#ifndef DONEITONCE
  #define DONEITONCE
  cout << "first time doing it" << endl;
#else
  cout << "did it allready" << endl;
#endif



显然,这不会编译.问题是我可以做类似的事情吗?
在编译期间更改了代码?我想像这样的东西:



Obviously that won''t compile. The question is can i do something similar,
that the code is changed during compile time? I imagined something like this:

bool done = false;
template<bool b="">;
void doit() {
  doit<B>();
}
template<>
void doit<false>() {
  cout << "first time doing it" << endl;
  done = true;
}
template<>;
void doit<true>() {
  cout << "did it allready" << endl;
}
#define DOIT doit<done>();</done></true></false></bool>



但这也会引发编译错误.
无论如何,函数调用应如下所示:



but that throws a compilation error, too.
Anyway the function call should look like:

DOIT        // first call
DOIT        // second call



输出如下:

第一次这样做
准备好了吗

我不想在< code> if(b)xxx;中执行此操作;其他yyy;</code>方式
我真的希望你能帮助我!
Tanks



with an output like:

first time doing it
did it allready

I do not want to do this in an <code>if (b) xxx; else yyy;</code> manner
I really hope you can help me!
Tanks

推荐答案

您在编译时说的时候让我感到困惑,这意味着构建指令,然后您需要创建一个变量或对象并绑定数据检查编译次数.

如果要计算运行时函数被访问的次数,则在撰写本文时,您在解决方案1和解决方案1中有两个答案. 2

[更新]
来自MSDN
http://msdn.microsoft.com/en-us/library/91621w01(VS .80).aspx [ ^ ]

来自Technet
http://technet.microsoft.com/en-us/library/3sxhs2ty.aspx [ ^ ]
You had me confused when you said at compile time, this would mean a build directive, then you would need to create a variable or object with data binding to check for the number of compile times.

If you meant to count number of times a function is accessed during run time, you have(at the time of writing) two answers in solutions 1 & 2

[Update]
From MSDN
http://msdn.microsoft.com/en-us/library/91621w01(VS.80).aspx[^]

From Technet
http://technet.microsoft.com/en-us/library/3sxhs2ty.aspx[^]


很抱歉,我的回答不是关于C ++的,但我的想法如此不寻常且通用,因此对您来说可能很有趣.
我的解决方案得到了很好的实施和测试;而且我肯定知道您可以使用此想法,因为从C ++中,您始终可以查看堆栈并记住调用点.

请看一下我的文章希望您在这里…只一次 [
Sorry that my answer is not about C++, but my idea is so unusual and universal so it could be interesting for you.

My solution is well implemented and tested; and I know for sure you can use this idea, because from in C++ you always can look at the stack and remember the calling point.

Please take a look at my article Wish You Were Here… Only Once[^].

—SA


如果您不担心线程安全:
If you''re not worried about thread safety:
void func()
{
    static bool first = true;

    if (first)
    {
        // Additional handling
        first = false;
    }

    // Normal execution
}



如果您担心线程安全,则必须保护使用的标志.



If you are worried about thread safety you will have to guard the flag used.


这篇关于在编译时修改函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆