[英]return static_cast<int> C++ and C
I would like to know about C and C++ language. 我想了解C和C ++语言。
C++ uses: C ++使用:
return static_cast<int>
How can I transform return static_cast<int>
to C? 如何将
return static_cast<int>
为C?
For example: 例如:
printf()
printf()
cout
cout
C style casting is simply prefixing the value with the type in parentheses. C样式转换只不过是用括号中的类型为值加上前缀。 Instead of
代替
static_cast<type>(value)
you simply do 你只是做
(type)value
eg 例如
static_cast<int>(x)
becomes 变成
(int)x
Or you could do 或者你可以做
#ifdef __cplusplus
#define STATIC_CAST(Type_, Value_) static_cast<Type_>(Value_)
#else
#define STATIC_CAST(Type_, Value_) (Type_)(Value_)
#endif
and use one invocation for both languages 并使用两种语言的一次调用
STATIC_CAST(int, x) // C++ static_cast<int>(x), C (int)(x)
The extra parentheses in the C version around the Value_ aren't required for simple cases but are there because this is a macro and if you said 简单情况下,不需要在C版本中使用Value_括起来的多余括号,但它们存在,因为这是一个宏,如果您说
STATIC_CAST(int, 1.0 + 2.0)
you don't want it to expand to 你不希望它扩展到
(int)1.0 + 2.0
but want it to expand to 但希望它扩展到
(int)(1.0 + 2.0)
Note that C++ allows the C form of casting but the templated cast mechanism is preferred by C++ engineers. 请注意,C ++允许C形式的转换,但是C ++工程师更喜欢模板化转换机制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.