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: 
There is no real way to enforce this at compilation time. This is something that the object itself will need to enforce with runtime checks
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();
    }
};

```