带有const限定符的自由函数类型的c++专门化
C++ specialization with free functions types with const-qualififier
不可能使自由函数具有const- qualified,但是下面的专门化是什么意思,什么时候应用?
template<typename _Res, typename... _ArgTypes>
struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
{ typedef _Res result_type; };
我可以这样使用这个专门化:
typedef _Weak_result_type_impl<int () const>::result_type type;
但是函数类型是"int () const"。什么时候使用?
这个const
可以用来捕获const
成员函数(正如0x499602D2所指出的)。
考虑下面的例子:
#include <iostream>
using namespace std;
class foo
{
public:
void bar1() { cout << "bar1n"; }
void bar2() const { cout << "bar2n"; }
};
template <typename T, void (T::*mf)() const>
struct Test
{
void call(T & obj) { (obj.*mf)(); }
};
int main()
{
foo f;
//Test<foo, &foo::bar1> t; // Doesn't compile
//t.call(f);
Test<foo, &foo::bar2> t2;
t2.call(f);
return 0;
}
模板Test
只能捕获const成员函数(否则不能编译)。您可以很容易地想象基于成员函数的连续性的专门化,这可能是您的代码正在做的(如果没有更多的上下文就无法判断)
现场演示
相关文章:
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 是否有任何建议来统一函数类型限定符并简化可恶的函数类型?
- 关于 C++ 中的函数类型定义
- 用于检测函数类型是否为否的特征
- 函数类型参数的模板参数推导
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- C++无效的函数类型转换
- STL 函数和函数类型与函数指针类型
- 如何将result_of与函数类型定义一起使用
- 将模板(没有规范)传递给 std::thread() 会出现错误:<未解析的重载函数类型>匹配错误
- C++ 编译错误:gnu_printf是无法识别的格式函数类型
- 专门用于"direct"函数类型(与函数指针类型相对)
- 将函数类型作为模板参数传递不会编译
- 通过参数传递 lambda(无函数类型模板)
- 如何在模板参数中分离函数类型返回类型和参数
- 为什么比较函数类型需要指定为模板参数?
- 带有限定符的函数类型定义用例
- 如何声明对函数类型的常量引用
- 非类型模板参数允许各种函数类型?