The Target Object






The Target Object

const type_info& function::target_type()  const;

The member function returns a reference to an object of type type_info that describes the type of the current target object.

Figure. function::target_type (funobjfun/targettype.cpp)

#include <functional>
#include <iostream>
#include <typeinfo>
#include <math.h>
using std::tr1::function;
using std::cout; using std::type_info;

struct S
  { // simple callable type
  float operator()(float) { return 1.0f; }
  typedef float result_type;
  };

void show_type(const char *title, const type_info& info)
  { // show name of target type
  cout << title << ": " << info.name() << '\n';
  }

int main()
  { // demonstrate function::target_type
  function<float(float)> fn;
  show_type("empty", fn.target_type());
  fn = cosf;
  show_type("cosf", fn.target_type());
  fn = S();
  show_type("S", fn.target_type());
  return 0;
  }

template <class Fty2> Fty2 *function::target();
template <class Fty2> const Fty2 *function::target() const;

The member functions return a pointer to the target object if the target object is of type Fty2; otherwise, they return a null pointer.

Each of these member functions is a function template. Since they do not take any arguments, the compiler can't deduce the template argument types, so you must give the argument type explicitly when you call these functions.

Figure. function::target (funobjfun/target.cpp)

#include <functional>
#include <iostream>
#include <typeinfo>
#include <math.h>
using std::tr1::function;
using std::cout; using std::type_info;

typedef function<float(float)> Fty;
typedef float(*fptr)(float);

struct S
  { // simple callable type
  float operator()(float) { return 1.0f; }
  typedef float result_type;
  };

void show_pointer(const char *title, Fty& fty)
  { // check pointer type and value
  cout << title << ": ";
  if (fty.target<fptr>())
    cout << " target has type pointer to function\n";
  else if (fty.target<S>())
    cout << " target has type S\n";
  else
    cout << " target is empty or has unknown type\n";
  }

int main()
  { // demonstrate function::target_type
  function<float(float)> fn;
  show_pointer("empty", fn);
  fn = cosf;
  show_pointer("cosf", fn);
  fn = S();
  show_pointer("S", fn);
  return 0;
  }



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows