フレンド演算子のイディオムを使用する:
struct Foo {
friend Foo operator+(Foo, Foo) { return {}; }
};
// which is synonymous to the slightly less pretty:
struct Bar {
friend Bar operator+(Bar, Bar); // optional
};
inline Bar operator+(Bar, Bar) { return {}; }
operator+基本的にforの関数ポインタが欲しいですFoo。
Bar私は次のように言うことができます:
auto fn = static_cast<Bar (*)(Bar, Bar)>(&operator+);
fn({},{});
ただし、Fooバージョンに対して同じことを行うと、g++ と clang++ から次のように通知されます。
// g++ 4.8.3
error: ‘operator+’ not defined
auto f = static_cast<Foo (*)(Foo, Foo)>(&operator+);
^
// clang++ 3.2-11
error: use of undeclared 'operator+'
auto f = static_cast<Foo (*)(Foo, Foo)>(&operator+);
^
これは本質的に不可能ですか、それとも関数を参照する方法はありますか?