template <class T>
T* target() noexcept;
template <class T>
const T* target() const noexcept;
概要
元となる関数を取得する。
要件
型T
が、ArgTypes...
型をパラメータにとり、R
を戻り値の型とする関数、または関数オブジェクトであること。
戻り値
target_type() == typeid(T)
ならば、保持している関数へのポインタを返す。そうれなければヌルポインタを返す。
例
#include <iostream>
#include <functional>
struct ident_functor {
int operator()(int x) const
{ return x; }
};
int ident_func(int x)
{ return x; }
int main()
{
// 関数オブジェクト
{
std::function<int(int)> f = ident_functor();
ident_functor* p = f.target<ident_functor>();
if (p) {
std::cout << (*p)(1) << std::endl;
}
}
// 関数ポインタ
{
std::function<int(int)> f = ident_func;
using fp_type = int(*)(int);
fp_type* p = f.target<fp_type>();
if (p) {
std::cout << (*p)(1) << std::endl;
}
}
}
xxxxxxxxxx
#include <iostream>
#include <functional>
struct ident_functor {
int operator()(int x) const
{ return x; }
};
int ident_func(int x)
{ return x; }
int main()
{
// 関数オブジェクト
{
std::function<int(int)> f = ident_functor();
ident_functor* p = f.target<ident_functor>();
if (p) {
std::cout << (*p)(1) << std::endl;
}
}
// 関数ポインタ
{
std::function<int(int)> f = ident_func;
using fp_type = int(*)(int);
fp_type* p = f.target<fp_type>();
if (p) {
std::cout << (*p)(1) << std::endl;
}
}
}
出力
1
1
バージョン
言語
- C++11
処理系
- Clang: 3.0 ✅
- GCC: 4.3.6 ✅
- Visual C++: ??