使用友元运算符习语:
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+);
^
这本质上是不可能的,还是有办法引用该函数?