c++ - 如何使用c++20概念匹配一个类的泛型成员函数
问题描述
我有一个模板类 Foo,包括一个可以接受任何 ARGS 的泛型函数 calc
template <class RET>
struct Foo
{
template<class ...ARGS>
RET calc(ARGS &&...args)
{
// do some thing...
((std::cout<<", "<<std::forward<ARGS>(args)), ...) <<std::endl;
return defaultRet;
}
RET defaultRet;
};
int main () {
auto ret = Foo<int>(42).calc(1,2,3); // print "1, 2, 3"
std::cout<< ret <<std::endl; // print "42"
return 0;
}
然后我想写一个概念Calcable
来匹配Foo
。然后Bar::calc
返回void
,Calcable<Bar<int>>
应该是这样false
但是我写不出来Calcable
,我疯了!
template <class RET>
struct Foo
{
template<class ...ARGS>
RET calc(ARGS &&...args)
{
// do some thing...
((std::cout<<", "<<std::forward<ARGS>(args)), ...) <<std::endl;
return defaultRet;
}
RET defaultRet;
};
template <class RET>
struct Bar
{
};
template <class T>
concept Calcable = ?? // <<<<<<<<<<<< How to match generics function calc?
template <Calcable T>
struct Check
{
};
int main () {
auto ret = Foo<int>(3).calc(1,2,3); // ok
std::cout<< ret <<std::endl; // ok
using YES = Check<Foo<int>>; // I need compile pass
using NO = Check<Bar<int>>; // I need compile error
return 0;
}
我一直在找它一整天,但没有
解决方案
推荐阅读
- druid - 僵尸巨人无法杀死的任务在重启时阻止德鲁伊
- jsf - 在评估动作EL表达式之后如何在JSF中触发命令按钮上的点击事件
- sql - 如何在flutter上查询sqflite多个whereArgs?
- python-3.x - TKinter 将用户名和密码保存在字符串文件中
- javascript - 通过键javascript对一组对象进行分组
- javascript - 将子 Vue 项目嵌入父 Vue 项目并访问父 Vuex 存储
- botframework - 在 MS Teams 客户端上无法访问内部托管的图像
- javascript - 需要帮助了解 Vue.js 功能
- antlr - 如何在单个规则中指定多个词法分析器规则?
- rust - 如何让结构将自身传递给另一个结构的函数