如何使模板函数像 R f<void(int)>(args...)
how to make a template function like R f<void(int)>(args...)
现在我想创建一个看起来像f<void(int)>(args...)
的函数,我的代码是:
template<typename T>
struct A{};
template<typename F>
A<F> f4();
template<typename R,typename... Args>
A<R(Args...)> f4<R(Args...)>() {
return A<R(Args...)>();
}
但它不起作用并且 vs 给出错误 C2768
我该怎么做?
您不能部分专用化函数模板; 类模板可以。
例如// primary template
template<typename F>
struct f4_s {
static A<F> f4() {
return A<F>();
}
};
// partial specialization
template<typename R,typename... Args>
struct f4_s<R(Args...)> {
static A<R(Args...)> f4() {
return A<R(Args...)>();
}
};
template<typename T>
auto f4() {
return f4_s<T>::f4();
}
然后
f4<int>(); // call the primary version
f4<int(int,char)>(); // call the specialization version
住
或者使用函数模板应用重载。
template<typename F>
std::enable_if_t<!std::is_function_v<F>, A<F>> f4() {
return A<F>();
}
template<typename F>
std::enable_if_t<std::is_function_v<F>, A<F>> f4() {
return A<F>();
}
然后
f4<int>(); // call the 1st overload
f4<int(int,char)>(); // call the 2nd overload
住
相关文章:
- C++程序在将 int 与 cin 一起使用时有效,但不能使用字符串
- 类中的 C++ int 被设置为值,似乎不知从何而来
- 不能使这种类型的"void(C::* volatile)(int) const "在参考手册C++示
- 如何使模板函数像 R f<void(int)>(args...)
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 使无符号的 int 值成为有符号的 int 值并返回
- 如何更改 int 以使其具有较小的字段来选择随机数
- 如何对 int 变量应用验证,使其仅接受整数数据,并且在任何其他数据的情况下不会出错?
- 如何使此函数使用 getline 读取字符串并使用 int 执行相同的行为?
- cdhtmldialog-使其模态
- 当将 getline 与 int 一起使用时,如何修复"没有重载函数 'getline' 的实例与参数列表匹配"
- 这个语句到底是什么意思 stack<int, list<int> > 它与 stack 有何不同<int>?
- 试图使我的程序将INT值从0更改为1
- C++:如何使 std::unique_ptr<int> 指向一个新整数?
- 如何避免int(xxx)_t之间的重叠,并使我的代码正确
- 无论如何,是否可以使用setCursorPos(int,int)函数,但没有采用两个int使它需要速度较慢的两倍
- 使用变换和加 () 对向量的元素进行求和和模 10^9+7<int>。有什么办法可以做到这一点吗?
- 如何在 C++ 中创建一个类,使其像本机 int 类一样工作
- 当 int 方法工作正常时,void 方法有何不同,或者为什么我不能调用 void 方法?
- 如何使g++为int i=i生成错误/警告