0
単純なテンプレートファクトリ関数を作成したいと思っています。ファクトリ関数は、テンプレートパラメータとファンクション引数を変数として取得します。functors演算子()を `decltype 'するには?
以下のコードです。問題はテンプレート化された関数です。コンパイル時に私は得た:
$ clang++ -std=c++14 test.cpp -o test
test.cpp:32:16: error: no matching function for call to 'factory'
std::cout << factory<variance_product_producer>(1.0) << std::endl;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:24:28: note: candidate template ignored: substitution failure [with T = variance_product_producer]: call
to non-static member function without an object argument
decltype(&T::operator()()) factory(Args... args) {
~~~~~~~~ ^
1 error generated.
私はそれを実行するために何を変更する必要がありますか?コード:
struct product
{
int i = 0;
};
struct complex_product_producer
{
complex_product_producer(float _setting)
: setting_(_setting)
{
}
product operator()()
{
product p;
p.i = 10;
return p;
}
float setting_;
};
template<typename T, typename... Args>
decltype(&T::operator()()) factory(Args... args) {
T t(args...);
return t();
}
#include <iostream>
int main()
{
std::cout << factory<complex_product_producer>(1.0) << std::endl;
}