インスタンスを識別するためのトレースの自動化については、次のいずれかを呼び出します。
- その識別子を返す包含オブジェクトの非静的メソッド
- 常に同じIDを返す何か
私の現在の解決策は、メソッド which() と、オブジェクトのコンテキストにない場合に使用する必要があるグローバル関数 which() を持つ基本クラスを持つことです。ただし、これは静的メンバー関数では機能しません。ここでは、コンパイラはグローバル メソッドよりも非静的メソッドを優先します。
簡単な例:
class IdentBase
{
public:
Ident(const std::string& id) _id(id) {}
const std::string& which() const { return _id; }
private:
const std::string _id;
};
const std::string& which() { static const std::string s("bar"); return s; }
#define ident() std::cout << which() << std::endl
class Identifiable : public IdentBase
{
public:
Identifiable() : Ident("foo") {}
void works() { ident(); }
static void doesnt_work() { ident(); } // problem here
};
静的メンバー関数の特別なマクロのような回避策の使用を回避することはできますか (テンプレート マジックを使用する可能性があります)。