I wanna build a simple templated factory-function, which just gets a functor as template parameter and the functors arguments as variables.
Below some code. The problem is the templated function. When compiling I got:
$ 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.
What should I change to make it run? The code:
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;
}
Aucun commentaire:
Enregistrer un commentaire