1

さまざまなアルゴリズムを保持するクラスがあります。

class Algorithm{

Algorithm()=delete;

public:
    template <typename IntegerType> 
    static IntegerType One(IntegerType a, IntegerType b);

    template <typename IntegerType> 
    static IntegerType Two(IntegerType a, IntegerType b);

    template <typename IntegerType> 
    static IntegerType Three(IntegerType a, IntegerType b);

    // ...
};

これらは次の方法で呼び出すことができます。

int main(){

    Algorithm::One(35,68);
    Algorithm::Two(2344,65);
    //...
}

ここで、「アルゴリズム」関数を取り、その関数を呼び出す前後に同じ手順を実行する関数を作成したいと考えています。
ここに私が持っているものがあります:

template <typename IntegerType>
void Run_Algorithm(std::function<IntegerType(IntegerType,IntegerType)>fun, IntegerType a, IntegerType b){
    //... stuff ...
    fun(a,b);
    //... stuff ...
    return;
}

次のように関数を呼び出そうとすると:

Run_Algorithm(Algorithm::One,1,1);

私が得るエラーは次のとおりです。

cannot resolve overloaded function ‘One’ based on conversion to type ‘std::function<int(int, int)>’

目的のアルゴリズムをパラメーターとして受け取る一般的なルーチンを設定するにはどうすればよいですか?

編集:
このソリューションは希望どおりに機能しました。次のようになります。

template <typename IntegerType>
void Run_Algorithm(IntegerType(*fun)(IntegerType, IntegerType), IntegerType a, IntegerType b){
    //... stuff ...
    fun(a,b);
    //... stuff ...
    return;
}
4

3 に答える 3

0

次のようなものが必要です。

template <template <typename T> class IntegerType>
void Run_Algorithm // ...
于 2013-09-08T13:57:57.007 に答える