7

我已经阅读了有关这方面的各种权威,包括Dewhurst,但还没有设法解决这个看似简单的问题。

我想要做的是调用一个 C++函数对象,(基本上,你可以调用的任何东西,一个纯函数或带有 () 的类),并返回它的值,如果它不是 void,否则返回“true”。

using std:

struct Foo {
  void operator()() { cout << "Foo/"l; }
};
struct Bar {
  bool operator()() { cout << "Bar/"; return true; }
};

Foo foo;
Bar bar;
bool baz() { cout << "baz/"; return true; }
void bang() { cout << "bang/"; }

const char* print(bool b) { cout << b ? "true/" : "false/"; }

template <typename Functor> bool magicCallFunction(Functor f) {
  return true;  // Lots of template magic occurs here 
                // that results in the functor being called.
}

int main(int argc, char** argv) {
  print(magicCallFunction(foo));
  print(magicCallFunction(bar));
  print(magicCallFunction(baz));
  print(magicCallFunction(bang));
  printf("\n");
}
// Results:  Foo/true/Bar/true/baz/true/bang/true

更新

感谢您的想法和想法!

基于此,我实际上决定将我所有的模板提升到一个级别 - 所以我有:

bool eval(bool (*f)()) { return (*f)(); }

bool eval(void (*f)()) { (*f)(); return true; }

template <typename Type>
bool eval(Type* obj, bool (Type::*method)()) { return (obj->*method)(); }

template <typename Type>
bool eval(Type* obj, void (Type::*method)()) { (obj->*method)(); return true; }

和泛型类来携带各种对象和方法。感谢 Ree 先生将我推向那个方向的代码!

4

6 回答 6

6

要在编译时检测 void 返回值,标准技巧是重载operator,. 逗号运算符最酷的地方在于它可以接受一个 void 参数,在这种情况下,它默认为内置的operator,. 在代码中:

template <typename> tag {};

template <typename T>
tag<T> operator,(T, tag<void>);

现在,即使有类型 void ,expr, tag<void>()也有类型。然后你可以用通常的技巧来捕捉它:tag<typeof(expr)>expr

char (&test(tag<void>))[1];
template <typename T> char (&test(tag<T>))[2];

template <typename F>
struct nullary_functor_traits
{
    static const bool returns_void = sizeof(test((factory()(), tag<void>()))) == 1;
private:
    static F factory();    
};
于 2011-04-05T14:24:35.353 回答
1

也许您可以使用 void& 作为类型没有意义但 void* 有意义的事实。

于 2010-12-26T22:12:53.080 回答
1

实现print(void)的重载无操作版本不是更容易 吗?

嗯嗯。函数模板和重载将在运行时轻松处理此问题。

如果您想在编译时处理这个问题,它会变得有点棘手,以便与#if 宏或静态编译时间断言一起使用。

但既然你只想要前者,我可以建议这样的起点:

(在 (GCC) 3.4.4 & 4.0.1 下测试。——我知道,我需要升级!)

#include <iostream>
using namespace std;

struct Foo {
  void operator()() {}
};
struct Bar {
  bool operator()() { return false; }
};
Foo foo;
Bar bar;
bool baz() { return false; }
void bang() {}


struct IsVoid
{
  typedef char YES[1];
  typedef char NO[2];

        /* Testing functions for void return value. */

  template <typename T>
  static IsVoid::NO  & testFunction( T (*f)() );

  static IsVoid::YES & testFunction( void (*f)() );

  static IsVoid::NO  & testFunction( ... );

        /* Testing Objects for "void operator()()" void return value. */

  template <typename C, void (C::*)()>
  struct hasOperatorMethodStruct { };

  template <typename C>
  static YES & testMethod( hasOperatorMethodStruct<C, &C::operator()> * );

  template <typename C>
  static NO & testMethod( ... );


        /* Function object method to call to perform test. */
  template <typename T>
  bool operator() (T & t)
  {
    return (    ( sizeof(IsVoid::testFunction(t))  == sizeof(IsVoid::YES) )
             || ( sizeof(IsVoid::testMethod<T>(0)) == sizeof(IsVoid::YES) ) );
  }
};


#define BOUT(X) cout << # X " = " << boolToString(X) << endl;

const char * boolToString( int theBool )
{
  switch ( theBool )
  {
    case true:   return "true";
    case false:  return "false";
    default:     return "unknownvalue";
  }
}

int main()
{
  IsVoid i;

  BOUT( IsVoid()(foo) );
  BOUT( IsVoid()(bar) );
  BOUT( IsVoid()(baz) );
  BOUT( IsVoid()(bang) );
  cout << endl;

  BOUT( i(foo) );
  BOUT( i(bar) );
  BOUT( i(baz) );
  BOUT( i(bang) );
}



好吧,我开始看到更多的问题。

虽然我们可以按照以下方式做一些事情:

#include <iostream>
using namespace std;

struct FooA {
  void operator()() {}
};
struct FooB {
  bool operator()() { return false; }
};
struct FooC {
  int operator()() { return 17; }
};
struct FooD {
  double operator()() { return 3.14159; }
};
FooA fooA;
FooB fooB;
FooC fooC;
FooD fooD;

void   barA() {}
bool   barB() { return false; }
int    barC() { return 17; }
double barD() { return 3.14159; }


namespace N
{
        /* Functions */

  template <typename R>
  R    run( R (*f)() )    { return (*f)(); }

  bool run( void (*f)() ) { (*f)();  return true; }


        /* Methods */

  template <typename T, typename R>
  R    run( T & t, R (T::*f)() ) { return (t .* f) (); }

  template <typename T>
  bool run( T & t, void (T::*f)() ) { (t .* f) (); return true; }
};


#define SHOW(X) cout << # X " = " << (X) << endl;
#define BOUT(X) cout << # X " = " << boolToString(X) << endl;

const char * boolToString( int theBool )
{
  switch ( theBool )
  {
    case true:   return "true";
    case false:  return "false";
    default:     return "unknownvalue";
  }
}


int main()
{
  SHOW( N::run( barA ) );
  BOUT( N::run( barA ) );
  SHOW( N::run( barB ) );
  BOUT( N::run( barB ) );
  SHOW( N::run( barC ) );
  SHOW( N::run( barD ) );
  cout << endl;

  SHOW( N::run(fooA,&FooA::operator()));
  BOUT( N::run(fooA,&FooA::operator()));
  SHOW( N::run(fooB,&FooB::operator()));
  BOUT( N::run(fooB,&FooB::operator()));
  SHOW( N::run(fooC,&FooC::operator()));
  SHOW( N::run(fooD,&FooD::operator()));
}

您仍然需要将&CLASS::operator()作为参数输入。


最终,虽然我们可以确定对象的operator()方法是否返回 void,但我们通常不能根据返回类型进行重载。

我们可以通过模板特化来绕过重载限制。但是后来我们陷入了这种丑陋,我们仍然需要指定类型......尤其是返回类型!手动或通过传入一个合适的参数,我们可以从中提取必要的类型。

顺便说一句:#define 宏也无济于事。像 ?: 之类的工具需要相同类型的 ? 和:部分。

所以这是我能做的最好的...



当然...

如果您不需要返回类型...

如果您只是将结果传递给另一个函数......

你可以这样做:

#include <iostream>
using namespace std;

struct FooA {
  void operator()() {}
};
struct FooB {
  bool operator()() { return false; }
};
struct FooC {
  int operator()() { return 17; }
};
struct FooD {
  double operator()() { return 3.14159; }
};
FooA fooA;
FooB fooB;
FooC fooC;
FooD fooD;

void   barA() {}
bool   barB() { return false; }
int    barC() { return 17; }
double barD() { return 3.14159; }


#define SHOW(X) cout << # X " = " << (X) << endl;

namespace N
{
  template <typename T, typename R>
  R    run( T & t, R (T::*f)() ) { return (t .* f) (); }

  template <typename T>
  bool run( T & t, void (T::*f)() ) { (t .* f) (); return true; }


  template <typename T>
  void R( T & t )
  {
    SHOW( N::run( t, &T::operator() ) );
  }

  template <typename T>
  void R( T (*f)() )
  {
    SHOW( (*f)() );
  }

  void R( void (*f)() )
  {
    (*f)();
    SHOW( true );
  }
};


int main()
{
  N::R( barA );
  N::R( barB );
  N::R( barC );
  N::R( barD );
  N::R( fooA );
  N::R( fooB );
  N::R( fooC );
  N::R( fooD );
}
于 2010-12-27T01:24:51.047 回答
0

使用 C++0x,您可以使用decltype.

于 2010-12-27T00:38:21.610 回答
0

如果你可以使用Boost,下面的代码可能会起作用。我认为所有的函数/函子都是无效的,就像你的问题一样。但是,为了使用它,result_type必须在所有仿函数(函数类)中定义。

#include <boost/utility/result_of.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp>
using namespace boost; // Sorry, for brevity

template< class F >
// typename result_of< F() >::type
typename disable_if<
  is_void< typename result_of< F() >::type >
, typename result_of< F() >::type
>::type
f( F const& x )
{
  return x();
}

template< class F >
typename enable_if<
  is_void< typename result_of< F() >::type >, bool
>::type
f( F const& x )
{
  x();
  return true;
}

template< class T >
T f( T x() )
{
  return x();
}

bool f( void x() )
{
  x();
  return true;
}

static void void_f() {}
static int int_f() { return 1; }

struct V {
  typedef void result_type;
  result_type operator()() const {}
};

struct A {
  typedef int result_type;
  result_type operator()() const { return 1; }
};

int main()
{
  A  a;
  V  v;
  f( void_f );
  f( int_f );
  f( a );
  f( v );
}

希望这可以帮助

于 2010-12-27T01:36:53.297 回答
-1

尝试专门针对 void 返回类型:

template<class F>
class traits;

template<class F, class T>
class traits<T (F)()>;

template<class F>
class traits<void (F)()>;

我认为 ...

于 2010-12-26T22:34:11.163 回答