私は、アルゴリズムのパフォーマンステストのためにさまざまなプロトタイプの関数を実行するために使用されるジェネリッククラスに取り組んでいます。
std::function
バインドされたものを実行できないため、スタックしました。エラーが発生した場所にコメント付きのサンプルコードを次に示します。
#include <utility>
#include <functional>
template<typename ReturnType>
class Performance
{
public:
template<typename... Args>
using Algorithm = std::function<ReturnType(Args...)>;
template<typename... Params>
void run(const Algorithm<Params...>& ref, const Algorithm<Params...>& target)
{
// ERROR: term does not evaluate as taking 0 args
ref();
target();
}
};
void bar1(int, int)
{
// algorithm 1
}
void bar2(int, int)
{
// algorithm 2
}
int main()
{
using test = Performance<void>;
using Algorithm = test::Algorithm<int, int>;
int x = 0;
int y = 1;
Algorithm ref = std::bind(bar1, std::ref(x), std::ref(y));
Algorithm target = std::bind(bar2, std::ref(x), std::ref(y));
test foobar;
foobar.run(ref, target);
}
問題は、std::function
タイプ、つまりAlgorithm
2つのパラメーター(タイプ付きint
)を取るように宣言されていることです。それらを呼び出すときは、2つの引数が必要です。
std::bind
適用後、返されるファンクターはパラメーターを取りません。引数(std::ref(x)
およびstd::ref(y)
)がバインドされています。Algorithm
として宣言する必要があります
using Algorithm = test::Algorithm<>;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加