用于空指针安全访问的C/ c++宏
C/C++ Macro for null safe pointer access
我想写一个C/c++宏null安全指针访问。我目前有这个,它工作得很好:
#define NULL_SAFE(p, e) if (p) p->e
NULL_SAFE(myPtr, myMethod(myArg));
但是我真正想要的是这样的东西:
NULL_SAFE(
myPtr, myMethod(myArg),
myOtherPtr, myOtherMethod(myOtherArg),
yetAnotherMyPtr, plsStopMethod(grArg),
...
);
展开为:
if (myPtr) myPtr->myMethod(myArg);
if (myOtherPtr) myOtherPtr->myOtherMethod(myOtherArg);
if (yetAnotherMyPtr) yetAnotherMyPtr->plsStopMethod(grArg);
我可以想到一大堆我可能喜欢使用的,但它们都是基于相同的概念。
这可能吗?这已经存在了吗?有什么建议吗?谢谢你的帮助!
如果NULL检查是算法的一部分,那么只需显式地键入NULL检查,而不需要任何繁琐的宏。
如果NULL检查是防御性编程的一种方式,那么正确的方法是assert(ptr);
。如果断言触发了,就去修复导致它的错误。重复此操作,直到没有bug,然后从生产质量代码中删除assert。
c++ 11:
inline void null_safe()
{
}
template <typename Ptr, typename Fn, typename... Args>
void null_safe(Ptr&& ptr, Fn&& fn, Args&&... args)
{
if (ptr)
fn();
// you could put "else" here
null_safe(std::forward<Args>(args)...);
}
你可以使用任何可调用对象作为第二个参数,所以:
int f2() {
return printf("f2n");
}
int f3() {
return printf("f3n");
}
int main()
{
int i1 = 1;
null_safe(
&i1, f2
);
null_safe(
NULL, f2,
&i1, f3
);
}
您也可以使用任何谓词作为第一个参数。
为什么这里是NULL而不是nullptr留给读者作为练习。
我能得到的最接近这是通过使用c++11可变变量模板,和C99可变变量宏…如果您的平台不允许这样做,那么很抱歉,不管怎样,编写代码是非常有趣的!
#include <functional>
#include <iostream>
template<class T>
void stuff(T a)
{
std::cout<< "stuff:" << a << std::endl;
}
template<class T>
void other_stuff(T a)
{
std::cout<< "other_stuff:" << a << std::endl;
}
template <typename Test, typename ToCall>
void tester(Test t, ToCall tc)
{
if(t) tc();
}
template <typename Test, typename ToCall, typename... Others>
void tester(Test t, ToCall tc, Others... args)
{
if(t) tc();
tester(args...);
}
#define FUN_WRAP(a,b) std::bind(a<decltype(b)>, (b) )
#define NULL_SAFE(...) tester(__VA_ARGS__)
int main()
{
NULL_SAFE(1, FUN_WRAP(stuff, 1),
0, FUN_WRAP(stuff, 2),
1, FUN_WRAP(other_stuff, 3)
);
}
相关文章:
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 运行时错误:引用绑定到类型为"int"的空指针
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 为什么我在空指针错误(链表)中获取成员访问权限
- 从向量到空指针的 memcpy(反之亦然)不起作用
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- 成员访问是否在空指针上定义C++?
- 尝试将对象插入空指针数组时出现分段错误
- 为什么我们需要在 C++ 中检查空指针,而在 Java 中不需要?
- 是否允许向空指针添加零?
- 为什么 C 样式字符串的工作空指针检查不?
- 在函数内初始化无符号字符指针将返回空指针
- 为什么多维数组中的空字符串文本衰减为空指针?
- C++ 取消引用指向矢量的空指针时的分段错误
- 错误 C6011:取消引用空指针"NAME"。C++
- cppcheck取消引用空指针
- 打印空指针时,std::cout 可以打印 "NULL" 而不是 0 吗?
- 递增空指针无法正确设置值
- Qt - 将空指针(原始数据)转换为 QImage 并将其显示在标签上
- 将多映射转换为空指针,然后转换回多映射