如果对象碰巧有它,如何调用成员函数?

时间:2012-08-18 01:55:43

标签: c++ templates sfinae

  

可能重复:
  Is it possible to write a C++ template to check for a function's existence?

我有一个函数f,它接收val类型的值T(模板化)。有什么方法可以在val 上调用成员函数,只有类型具有这样的成员函数?

示例:

struct Bar {
  void foo() const {}
};

template<class T>
void f(T const& val) {
  // Is there any way to call *only* if foo() is available on type T?                                                           
  // SFINAE technique?                                                                                                          
  val.foo();
}

int main() {
  Bar bar;
  f(bar);
  f(3.14);
}

对我来说听起来像SFINAE技术,可能使用boost :: enable_if,但我不知道如何让它在这里工作。请注意,我无法轻松更改示例中的Bar类型。我知道如果Bar包含一些特定的typedef等会很容易,这表明该函数可用。

毋庸置疑,我不知道将T调用的f类型集合。有些具有foo成员函数,有些则没有。

2 个答案:

答案 0 :(得分:4)

您可以执行此操作,如下面的测试程序所示(使用 GCC 4.7.0或clang 3.1)。

静态模板函数has_void_foo_no_args_const<T>::eval(T const & t) 如果方法t.foo()存在且公开,则会调用void T::foo() const。它会 如果没有这样的方法,什么都不做。 (当然编译错误会 如果方法是私有的,则得到结果。)

该解决方案适用于并从方法内省中扩展而来 模板我提供了here。您 可以阅读该答案,以解释SNIFAE逻辑如何工作, 并且还要了解如何将该技术推广到参数化 您正在探测的函数签名的属性。

#include <iostream>

/*! The template `has_void_foo_no_args_const<T>` exports a
    boolean constant `value` that is true iff `T` provides
    `void foo() const`

    It also provides `static void eval(T const & t)`, which
    invokes void `T::foo() const` upon `t` if such a public member
    function exists and is a no-op if there is no such member.
*/ 
template< typename T>
struct has_void_foo_no_args_const
{
    /* SFINAE foo-has-correct-sig :) */
    template<typename A>
    static std::true_type test(void (A::*)() const) {
        return std::true_type();
    }

    /* SFINAE foo-exists :) */
    template <typename A> 
    static decltype(test(&A::foo)) 
    test(decltype(&A::foo),void *) {
        /* foo exists. What about sig? */
        typedef decltype(test(&A::foo)) return_type; 
        return return_type();
    }

    /* SFINAE game over :( */
    template<typename A>
    static std::false_type test(...) {
        return std::false_type(); 
    }

    /* This will be either `std::true_type` or `std::false_type` */
    typedef decltype(test<T>(0,0)) type;

    static const bool value = type::value; /* Which is it? */

    /*  `eval(T const &,std::true_type)` 
        delegates to `T::foo()` when `type` == `std::true_type`
    */
    static void eval(T const & t, std::true_type) {
        t.foo();
    }
    /* `eval(...)` is a no-op for otherwise unmatched arguments */ 
    static void eval(...){
        // This output for demo purposes. Delete
        std::cout << "T::foo() not called" << std::endl;        
    }

    /* `eval(T const & t)` delegates to :-
        - `eval(t,type()` when `type` == `std::true_type`
        - `eval(...)` otherwise
    */  
    static void eval(T const & t) {
        eval(t,type());
    }
};

// For testing
struct AA {
    void foo() const {
        std::cout << "AA::foo() called" << std::endl;
    }
};

// For testing
struct BB {
    void foo() {
        std::cout << "BB::foo() called" << std::endl;
    }
};

// For testing
struct CC {
    int foo() const {
        std::cout << "CC::foo() called" << std::endl;
        return 0;
    }
};

// This is the desired implementation of `void f(T const& val)` 
template<class T>
void f(T const& val) {
    has_void_foo_no_args_const<T>::eval(val);
}

int main() {
    AA aa;
    std::cout << (has_void_foo_no_args_const<AA>::value ? 
        "AA has void foo() const" : "AA does not have void foo() const")
        << std::endl; 
    f(aa);
    BB bb;  
    std::cout << (has_void_foo_no_args_const<BB>::value ? 
        "BB has void foo() const" : "BB does not have void foo() const")
        << std::endl;
    f(bb);
    CC cc;  
    std::cout << (has_void_foo_no_args_const<CC>::value ? 
        "CC has void foo() const" : "CC does not have void foo() const")
        << std::endl;
    f(cc);  
    std::cout << (has_void_foo_no_args_const<double>::value ? 
        "Double has void foo() const" : "Double does not have void foo() const")
        << std::endl;
    f(3.14);
            return 0;

}

该程序输出:

AA has void foo() const
AA::foo() called
BB does not have void foo() const
T::foo() not called
CC does not have void foo() const
T::foo() not called
Double does not have void foo() const
T::foo() not called

答案 1 :(得分:0)

如果你有一个typedef bool has_f,或者一个静态(在编译时可用的东西),我想你可以分支(使用boost元编程函数),或者提供2个模板,如果has_f被定义为另一个模板将会实例化如果不是。没有关于T,恕我直言的元数据,它直接感觉不可行。

相关问题