在C++中有没有办法确定可调用对象的函数签名?
考虑以下:
template< typename F >
void fun(F f)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
让我们假设fun只用可调用的"东西"来调用它.
在里面fun我想知道什么是功能的签名f.这应该适用于函数指针,引用,包装器,lambdas,绑定,函数对象(假设它们只有一个operator ())等等.我受限于Visual Studio 2010 SP 1,但即使不使用该编译器,我也对标准解决方案感兴趣.
(函数签名Return_Type ([Arg1_Type [, Arg2_Type [, ... ] ] ]);与std::function/ boost::function.相同)
知道至少返回值的部分解决方案具有f一定的价值.(我已经尝试std::result_of但无论如何都无法让它工作.)
考虑重载函数
void f(int);
void f(long);
void f(double);
void f(MyClass);
Run Code Online (Sandbox Code Playgroud)
以及未知参数类型的模板类中的方法
template <class T>
struct C {
void method(T arg) { ... }
};
Run Code Online (Sandbox Code Playgroud)
我想在编译时检查是否有一个版本f可以arg作为参数。
template <class T>
struct C {
void method(T arg) {
if constexpr (CAN_BE_CALLED(f, arg)) {
f(arg);
} else {
g();
}
}
};
Run Code Online (Sandbox Code Playgroud)
如何static_assert在C++ 11中使用EqualityComparable概念模板类型?
c++ ×3
arguments ×1
c++11 ×1
equality ×1
function ×1
parameters ×1
templates ×1
terminology ×1