How to `decltype` a functors operator()?
How to `decltype` a functors operator()?
我想构建一个简单的模板工厂功能,它只是将函子作为模板参数,而函数参数为变量。
以下一些代码。问题是模板函数。编译时,我得到了:
$ 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;
}
尝试
template<typename T, typename... Args>
auto factory(Args... args) -> decltype(T{args...}()) {
T t{args...};
return t();
}
但更改main()
如下
int main()
{
auto p = factory<complex_product_producer>(1.0f);
std::cout << p.i << std::endl;
}
或为product
operator<<
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- 'operator='已弃用:改用 QDir::setPath()
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 过载'operator new'如何导致无限循环?
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- How to `decltype` a functors operator()?
- 对简单的帐户检查程序有问题?程序返回编译错误,指出"no match for 'operator||' unsure of how to fix?"
- SWIG C++ to Python: Warning(362): operator= ignored
- 为什么"dumb to override the & operator and return *this*"?
- 使用 C++11 编译 UI 代码时面临"unable to find string literal operator"错误