static_cast
で使用するのファンクター バージョンを実装しようとしていますstd::bind()
。
Boost は知っていますが( boost::bind で static_cast を使用するをll_static_cast<K>()
参照)、現在は Boost を使用していません。
Why do some of the standard operator not have standard functor?にコード例があります。しかし、GCC 4.2.1 ではコンパイルできません:
template <typename Target>
struct StaticCast
{
template <typename Source>
Target operator()(Source&& source) const
{
return static_cast<Target>(source);
}
}
なんとかコンパイルすることができましたが、それが正しいかどうかはわかりません:
template <class Target>
struct StaticCast : public std::unary_function<void, Target> {
template <class Source>
Target operator()(Source& src) const {
return static_cast<Target>(src);
}
};
このバージョンが正しいかどうか誰か教えてもらえますか? もしそうなら、なぜstd::unary_function
前のコード例で使用されていないものが必要なのですか?
使用法:
std::vector<BaseObject*> vec; // BaseObject* are known to be of type
// DerivedObject* of course, please don't ask me how or why...
std::for_each(vec.begin(), vec.end(),
std::bind(&DerivedObject::doStuff,
std::bind(StaticCast<DerivedObject*>(), std::placeholders::_1),
"with some string"));