有条件的经营者具有恒定(真)值? [英] Conditional operator with a constant (true) value?

查看:96
本文介绍了有条件的经营者具有恒定(真)值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看着OpenSSL中使用的一些preprocessor宏和我碰到下面从加密/堆栈/ safestack.h

I was looking at some preprocessor macros used in OpenSSL, and I came across the following from crypto/stack/safestack.h:

#define CHECKED_STACK_OF(type, p) \
    ((_STACK*) (1 ? p : (STACK_OF(type)*)0))

#define CHECKED_SK_FREE_FUNC(type, p) \
    ((void (*)(void *)) ((1 ? p : (void (*)(type *))0)))

#define CHECKED_SK_FREE_FUNC2(type, p) \
    ((void (*)(void *)) ((1 ? p : (void (*)(type))0)))

<击>我猜它编写的方式来解决一个编译器错误(可能是古代的东西还没有得到供应商支持在过去十年)。

什么是使用的目的 1 上面,因为它总是真的吗?

What is the purpose of using the 1 above since its always true?

推荐答案

这是双重检查正确的类型传递code。指针p被传递并沿该指针的类型也必须在宏手动键入。

It's code that double checks if the correct type is passed. The pointer p is passed and along the type of that pointer must be also manually typed in the macro.

三元前pression总是返回第二个操作数但无论第二个和第三个操作数会,如果他们的类型匹配检查,如果他们不这样做,你应该得到一个编译错误。

The ternary expression will always return the second operand but both second and third operands will be checked if their type matches, and if they don't you should get a compiler error.

一个简单的例子:

int* p = NULL ;

1 ? p : ( float* )p ;    //error

1 ? p : ( int* )p ;      //ok

这篇关于有条件的经营者具有恒定(真)值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆