如何確保在編譯時調用特定的方法?在編譯時確保調用方法
例如,假設我有2種方法的對象:
struct Foo
{
... func1(...);
... func2(...);
};
,我想確保FUNC1到FUNC2任何調用之前被調用,即:
int main()
{
Foo f;
...
f.func1(...);
f.func2(...);
f.func2(...); // and so on
}
但我想生成編譯錯誤,如果我做這樣的事情:
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
}
您可以嘗試使用'constexpr',但在這種情況下不起作用。 (不保證在任何情況下都能正常工作)。 –
我想這隻能用代碼分析儀完成。編譯器本身只能警告你一個未使用的函數 - AFAIK。 –
如果你需要這樣做,你應該不斷思考一個更好的課堂設計。這是一個順序耦合反模式:http://en.wikipedia.org/wiki/Sequential_coupling –