指向非静态成员变量作为模板参数的指针
Pointer to non static member variable as template parameter
看起来问题6880832对我的问题有答案,但它没有回答如何在模板化类中引用指针。我已经走到这里了:
template<typename C, typename T, T C::*m, int direction>
class Cmp {
private:
bool isAscend = direction;
public:
bool operator()(const C* lhs, const C* rhs) {
return isAscend ?
rhs->m > lhs->m :
lhs->m > rhs->m;
}// bool operator()(const UnRecTran* lhs,const UnRecTran* rhs)
};// class Cmp
Cmp<UnRecTran, shrtDate, &UnRecTran::date, true>
(我试图在此特定实例化中对UnRecTran::date值进行比较)。然而,我得到"'m':不是'UnRecTran'的成员"。
我想做的是可能的吗?我理解成员变量的"地址"是常量——它只是对象开始的偏移量,而不是物理(运行时)地址。
通过指向成员的指针访问成员数据的语法是:
obj.*m_ptr //obj is class type
p_obj->*m_ptr //p_obj is pointer to obj
你的操作符应该是这样的:
bool operator()(const C* lhs, const C* rhs) {
return isAscend ?
rhs->*m > lhs->*m :
lhs->*m > rhs->*m;
}
相关文章:
- 使用指向成员的指针将成员函数作为参数传递
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- 修改函数中的指针(将另一个指针作为参数传递)
- Clang bug?使用指针作为模板参数
- MSVC将仅移动结构参数解释为指针
- 非类型指针和引用模板参数,以及在编译时如何/为什么解析它们.c++
- 如何使用非类型模板参数传入指向数组的指针
- 将成员函数指针作为参数传递给模板方法
- 匹配函数指针作为模板参数?
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 通过智能指针和转换对基本模板参数进行模板推导
- 为函数定义符号不明确的指针参数
- 使用引用与指针将数组作为参数传递
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- C++ - 声明指向返回任何类型并获取任意数量参数的函数的指针
- 将函数参数完美转发到函数指针:按值传递呢?
- 保留函数指针模板参数
- 如何构造一个以可变参数指针函数作为成员的类?
- 指向对象的非类型模板参数指针
- c++ 使用 *& as 参数(指针乐趣,三级树方法)