简体   繁体   English

“ static_cast”有什么用 <void> 在宏中?

[英]what is the use of “static_cast<void>” in macro?

I'm seeing a macro definition like this: 我看到这样的宏定义:

#define ASSERT_VALID_PARAM(param, assertion) {  static_cast<void>(param); if (!(assertion)) { throw InvalidParamError(#param, #assertion, __FILE__, __PRETTY_FUNCTION__, __LINE__); } }

I'm not able to figure out the need of static_cast<void>(param) here. 我无法在这里找出对static_cast<void>(param) Any idea on why this is needed? 对为什么需要这样做有任何想法吗?

This macro is designed to validate a certain real parameter passes a certain validation rule(s). 此宏旨在通过某些验证规则来验证某个参。 The logic part of the macro is composed of 2 parts: 宏的逻辑部分由2部分组成:

  1. Validate that param is a real parameter, with a valid name. 验证param是带有有效名称的真实参数。 This is done by using the static_cast , and if an illegal name is used, a compile time error will be generated. 这可以通过使用static_cast ,如果使用了非法名称,则会生成编译时错误。
  2. Validate the "truthyness" of assertion . 验证assertion的“真实性”。 This is done with a simple negating if statement. 这是通过简单的否定if语句完成的。

If the param is a valid name, and the assertion fails ( assertion == false ), an InvalidParamError is thrown, using the passed in parameters as strings (using the Stringizing operator # ) to initialize the error object. 如果参数是有效名称,并且断言失败( assertion == false ),则使用传入的参数作为字符串(使用Stringizing运算符# )来初始化InvalidParamError ,以初始化错误对象。

Since the actual usage of the param parameter in the macro is only as a string, it has to be validated using actual code. 由于宏中param参数的实际用法仅是字符串,因此必须使用实际代码进行验证。 Since no real operation is needed the static_cast is used, which discards the result and can potentially be optimized out. 由于不需要实际操作,因此使用static_cast,它会丢弃结果,并且有可能进行优化。 Without that check, you could pass any value which would make the information in the assertion meaningless. 如果没有该检查,则可以传递任何值,该值将使断言中的信息毫无意义。

it is the 'c++ way' of writing 这是“ C ++”的写作方式

(void)param;

it makes 'use' of the variable and thus disables the compiler warning for unused variable 它使变量“使用”,从而禁用未使用变量的编译器警告

static_cast<void>(param); will evaluate the param and discard the result. 将评估param并丢弃结果。

If you don't add the cast to void : 如果您不将演员表添加到void

  1. you may get warnings saying you are ignoring the result of expression. 您可能会收到警告,表示您忽略了表达式的结果。
  2. Even if you pass some illegal code (for example a statement instead of expression) as argument, compiler will accept it happily. 即使您传递一些非法代码(例如,用语句代替表达式)作为参数,编译器也会很乐意接受它。

From cppreference 来自cppreference

4) If new_type is the type void (possibly cv-qualified), static_cast discards the value of expression after evaluating it. 4)如果new_type是void类型(可能是cv限定),则static_cast在对表达式的值求值后将其丢弃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用它还是static_cast <void*> 然后是static_cast <myType*> 避免重新解释? - should I use it or static_cast<void*> then static_cast<myType*> to avoid reinterpret_cast? 什么是`static_cast <volatile void> “对于优化器来说意味着什么? - What does `static_cast<volatile void>` mean for the optimizer? 为什么我可以对static * cast使用void *但不能对char *使用 - Why can I use static_cast With void* but not With char* SFINAE:“ static_cast <void> ()”或“,void()”? - SFINAE: 'static_cast<void>()' or ', void()'? reinterpret_cast的 <int*> (char *)与static_cast <int*> (的static_cast <void*> (char *)) - 使用哪个? - reinterpret_cast<int*>(char*) vs. static_cast<int*>(static_cast<void*>(char*)) — which to use? 这里“int []”的目的是什么:“std::void_t <int[static_cast<int> (-1) &lt; static_cast<int> (0)]&gt;;" </int></int[static_cast<int> - What is the purpose of "int[]" here: "std::void_t<int[static_cast<Int>(-1) < static_cast<Int>(0)]>;" 使用* void作为static_cast的缓冲区 - using *void as a buffer for static_cast 使用static_cast检查void * to Object是否成功 - Check if void* to Object with static_cast is successful Union vs. static_cast(void*) - Union vs. static_cast(void*) C ++ static_cast <void *> - C++ static_cast<void *>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM