Question: How can I ensure that a specific method is called, at compile time?

For example, suppose I have an object with 2 methods:

```
struct Foo
{
   ... func1(...);
   ... func2(...);
};

```

and I want to ensure that func1 is called before any calls to func2, i.e.:

```
int main()
{
   Foo f;
   ...
   f.func1(...);
   f.func2(...);
   f.func2(...); // and so on
}

```

but I want to generate a compile error if I do something like that:

```
int main()
{
   Foo f;
   ...
   f.func2(...);  // generate a compile error due the fact that func1 must be called first
   f.func1(...);
   f.func2(...); // and so on
}

```

Answer: 
One method that comes to mind, is to let return func1() an object, that acts like a proxy to func2():

```
class Foo {
public:
    Proxy func1();   
private:
    void func2();
    friend class Proxy;
};

class Proxy {
private:
    explicit Proxy(Foo& f) : f_(f) {}
public:
    void func2() {
        f_.func2();
    }
    friend class Foo;
};

Foo f;
f.func1().func2();

```

An other method (and my favorite) is to let func1() be a constructor or to use an other class, that calls func1() in it's constructor:

```
class Foo
{
private:
    void func1(), func2();
    friend class FooUser;
};

class FooUser
{
public:
    explicit Proxy(FooUser& f) : f_(f) {
        f.func1();
    }

    void func2() {
        f_.func2();
    }
};

```
I really do not suggest to do so, but if you need it for debugging purposes you can try following.
Change every call func1() to

```
#define FLAG
func1();

```

and func2() to

```
#ifdef FLAG
func2();
#else
#error func1 should be called first!
#endif

```

So you will receive a compile-time error if func2() will be mentioned upper in text then func1. It does not mean it will be really called earlier during execution.