模板:只有在类有的情况下才执行方法

Tra*_*isG 6 c++ templates template-function

我想编写一个执行某个模板化类的方法的函数,但如果该类没有它,也应该编译好.在这种情况下,它应该不会调用该函数.

struct A
{
   void func() {}
};

struct B
{
};

template <typename T>
void anotherFunc(T t)
{
   //do t.func() here if T implements func, just do nothing if it doesn't.
}
Run Code Online (Sandbox Code Playgroud)

这有可能吗?

Yak*_*ont 6

// type_sink takes a type, and discards it.  type_sink_t is a C++1y style using alias for it
template<typename T> struct type_sink { typedef void type; };
template<typename T> using type_sink_t = typename type_sink<T>::type;

// has_func is a traits class that inherits from `true_type` iff the expression t.func()
// is a valid one.  `std::true_type` has `::value=true`, and is a good canonical way to
// represent a compile-time `bool`ean value.
template<typename T,typename=void> struct has_func : std::false_type {};
template<typename T> struct has_func<
  T,
  type_sink_t< decltype( std::declval<T&>().func() ) >
> : std::true_type {};

// helpers for tag dispatching.
namespace helper_ns {
  template<typename T> void anotherFunc( T&& t, std::false_type /* has_func */ ) {}
  template<typename T> void anotherFunc( T&& t, std::true_type /* has_func */ ) {
    std::forward<T>(t).func();
  }
}
// take the type T, determine if it has a .func() method.  Then tag dispatch
// to the correct implementation:
template<typename T> void anotherFunc(T t) {
  helper_ns::anotherFunc( std::forward<T>(t), has_func<T>() );
}
Run Code Online (Sandbox Code Playgroud)

是一个C++ 11解决方案,它在traits类上执行标记调度,以确定if t.func()是否为有效表达式.

  • @JanHudec这是一个很好的解决方案,但我期待一些更像惯用的东西,比如`template <typename T> auto helper(T && t) - > decltype(std :: declval <T>().f(),void()){ TF(); } void helper(...){}` (4认同)