如何在编译时测试模板函数是否存在
How to test if template function exists at compile time
我有以下模板函数
template<class Visitor>
void visit(Visitor v,Struct1 s)
{
}
如何在编译时使用SFINAE检查此函数是否存在
没有更多的细节,我只能猜测你有什么可用的,但这里有一个可能的解决方案:
//the type of the call expression to visit with a given Visitor
//can be used in an SFINAE context
template <class Visitor>
using visit_t = decltype(visit(std::declval<Visitor>(), std::declval<Struct1>()));
//using the void_t pattern
template <typename Visitor, typename=void>
struct foo
{
void operator()(){std::cout << "does not exist";}
};
template <typename Visitor>
struct foo<Visitor,void_t<visit_t<Visitor>>>
{
void operator()(){std::cout << "does exist";}
};
实时演示(只需删除-DDEFINE_VISIT以查看输出开关)
相关文章:
- 函数是否可以访问传递给main()的参数
- 根据某个函数是否存在启用模板
- 无论如何,我可以确定构造函数是否存在吗?
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 如何检查函数是否在LLVM Instrumentation pass的ModulePass的系统头文件中定义?
- 如果 C 函数仍然可以间接执行(通过回调函数),那么将它声明为静态函数是否是一种不好的做法?
- 在对象构造期间,将指向尚未构造的子对象的指针传递给另一个子对象的构造函数是否危险?
- 某些 boost::asio 异步函数是否将处理程序连接到操作,以便处理程序被触发一次?
- 调用 erase() 函数是否也会在擦除元素之前更改迭代器值?
- 由并发无序映射查找线程调用的函数是否安全?
- 使用静态成员函数而不是普通函数是否有任何开销?
- 从其存储的回调中删除 std::函数是否安全
- 析构函数是否会自动调用 delete[] C++?
- 构造函数是否有一种现代C++方法来了解其'container'类?
- 循环中本地对象的析构函数是否保证在下一次迭代之前被调用?
- 移动构造函数是否C++过时?
- 在 c++ 中将对象设置为等于同一类的构造函数是否有效?
- 此函数是否会在C++中创建内存泄漏?
- 具有默认值的单个参数构造函数是否与默认构造函数相同?
- 如何检测构造函数是否与抛出的析构函数无关