Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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


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
Posted
Comments
Sergey Alexandrovich Kryukov 10-Jun-11 18:57pm    
"In compile time" is wrong. It will be in run time, whatever you do.
--SA

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[^]
 
Share this answer
 
v2
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
 
Share this answer
 
v2
If you're not worried about thread safety:
C++
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.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 10-Jun-11 18:59pm    
Exactly, my 5.

Please also take a look at my solution which I put mostly for fun. But for .NET this is absolutely universal and well-polished and tested solution, for robustness and performance, but also... funny.
Just take a look at my solution and article (have you seen it before?)...

--SA
Niklas L 11-Jun-11 4:18am    
Yes I did read it before, and found it quite entertaining. A very innovative approach indeed.
Why don't you use, like us common mortal, a static variable? e.g.
C++
void doit()
{
  static bool already_done= false;

  if (already_done)
    cout << "Already done." << endl;
  else 
    cout << "First time." << endl;

  already_done = true;
}
 
Share this answer
 
Tank all of you for the fast response!
But I still would like the compiler to check for the first function call not the function itself, so the desired compiler behaviour would be like:

"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"

Thats more the thing I intended.

Though I'm not very familiar with that topic. Are there any good resources in the web? Using Visual Studio 2005 pro
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900