"check if member exists using enable_if"中修改的代码不起作用
Modified code from "check if member exists using enable_if" is not working
我读了本文。检查成员是否使用enable_if存在我像这样修改了Johanness Schaub的代码。
//////////////////////////////////////////////////////////////////////////
struct NormalChecker {
struct general_ {};
struct special_ : general_ {};
template<typename> struct int_ { typedef int type; };
template<typename Lhs>
void modify(Lhs &&lhs) {
cout << "modifyrn";
modifyNormal(lhs, special_());
}
template<typename Lhs, typename int_<decltype(Lhs::normal)>::type = 0>
void modifyNormal(Lhs &&lhs, special_) {
cout << "modifyNormal with normalrn";
}
template<typename Lhs>
void modifyNormal(Lhs &&lhs, general_) {
cout << "modifyNormal without normalrn";
}
};
struct DataWithNormal {
int normal;
};
struct DataWithoutNormal {
};
int main() {
DataWithNormal with_normal;
DataWithoutNormal without_normal;
NormalChecker normalCheckerWithNormal;
normalCheckerWithNormal.modify(with_normal);
NormalChecker normalCheckerWithoutNormal;
normalCheckerWithoutNormal.modify(without_normal);
return 0;
}
,但是,它只是两次说"不正常"。我错过了什么?
Lhs
被推论为示例中的参考类型,特别是 DataWithNormal&
。参考类型没有嵌套的normal
。解决此问题的一种方法是在剥离引用时检查Lhs
:
decltype(std::remove_reference<Lhs>::type::normal)
提起伊戈尔的评论,您也可以假装自己有一个对象,因为访问对象的成员即使有参考:
:decltype(std::declval<Lhs>().normal)
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 独立读取-修改-写入顺序
- 当系统的卷被修改时,如何修改WASAPI环回捕获卷
- Insert函数不适用于2 if语句C++
- 修改函数中的指针(将另一个指针作为参数传递)
- If语句未被求值C++
- 为什么我可以通过引用修改常量返回
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 对于结构,表达式必须是可修改的ivalue
- "check if member exists using enable_if"中修改的代码不起作用
- 在 if.else 中修改 typedef 声明
- 任何人都可以修改以下代码以避免使用"if"吗?
- 使用if()根据属性修改类行为是否是代码味道