1

私はこのコードで動作しているように見えます:

template <typename C>
class class_ {
protected:
    std::map<std::string, native_function> methods;
public:
    template <typename F, F fn>
    class_ &method(const std::string &name) {
        methods[name] = method_helper<C, F>::template toNative<fn>();
        return *this;
    }
};

これにより、次のことが可能になります。

class_<MyClass>()
    .method<decltype(&MyClass::numRows), &MyClass::numRows>("numRows");

ただし、エクスポートされたクラスに非メンバー関数をメソッドとして追加できるようにしたいと考えています。method問題は、通常の関数ポインターを操作するには、別の定義が必要なことです。

template <F, F fn>
class_ &method(const std::string &name) {
    methods[name] = function_helper<F>::template toNative<fn>();
    return *this;
}

ただし、上記のように、テンプレート パラメーターはまったく同じです。

まったく異なる名前の関数を作成する以外に、関数ポインターとメンバー関数ポインターを区別する便利な方法はありますか? または、実行時にどのコードを実行するかを決定する方法はありますか?

4

2 に答える 2

2

SFINAE を使用:

template<
    typename F
    , F fn
    , typename std::enable_if<
        std::is_member_pointer<F>::value
        , int
    >::type...
>
class_ &method(const std::string &name)
{
    methods[name] = method_helper<C, F>::template toNative<fn>();
    return *this;
}

template<
    typename F
    , F fn
    , typename std::enable_if<
        !std::is_member_pointer<F>::value
        , int
    >::type...
>
class_ &method(const std::string &name)
{
    methods[name] = function_helper<F>::template toNative<fn>();
    return *this;
}
于 2012-10-13T00:27:58.600 に答える
0

あなたの例の非メンバー関数ポインターには、次の署名があります。

template<typename Ret, typename Args...> Ret (*)(Args args...)

署名を指すメンバ関数ポインタ:

template<typename Ret, typename Args...> Ret (C::*)(Args args...)

これにより、次の 2 つの署名に特化できます。

template <typename F, F fn>
class_ &method(const std::string &name) { return method_impl(name, fn); }

template <typename F>
class_ &method_impl(const std::string &name, F fn)
{
    // non-member function variant.
}

template <typename Ret, typename Args...>
class_ &method_impl(const std::string &name, Ret (C::*fn)(Args args...))
{
    // member function variant.
}

注: に変更method_implするとmethod、API を簡素化して、次のことが可能になります。

class_<MyClass>().method("numRows", &MyClass::numRows);
于 2012-10-12T22:39:44.600 に答える