我在/usr/include/linux/kernel.h中碰到了这个奇怪的宏代码:
/* Force a compilation error if condition is true, but also produce a
result (of value 0 and type size_t), so the expression can be used
e.g. in a structure initializer (or where-ever else comma expressions
aren't permitted). */
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
Run Code Online (Sandbox Code Playgroud)
怎么:-!!
办?
我在编码书中看到了以下宏定义.
#define TRUE '/'/'/'
#define FALSE '-'-'-'
Run Code Online (Sandbox Code Playgroud)
那里没有解释.
请向我解释如何将这些作为工作TRUE
和FALSE
.
阅读Paul Graham关于编程语言的论文,人们会认为Lisp宏是唯一可行的方法.作为一个忙于开发人员,在其他平台上工作,我没有使用Lisp宏的特权.作为想要了解嗡嗡声的人,请解释是什么让这个功能如此强大.
还请将此与我从Python,Java,C#或C开发世界中理解的内容联系起来.
很抱歉提出非常基本的问题.我想在#ifdef指令中设置OR条件.怎么做 ?我试过了
#ifdef LINUX | ANDROID
...
..
#endif
Run Code Online (Sandbox Code Playgroud)
那没起效?什么是正确的方法?
显然从Visual Studio 2012中删除了宏.
是否有插件/扩展/工具可以让我录制和播放键盘宏(很像Visual Studio 2010中的录制/播放临时宏)?
例如,我通常会在将代码从一种语言转换为另一种语言时使用宏,或者从文本列表中快速生成属性等.
macros visual-studio-2013 visual-studio-2015 visual-studio-2017 visual-studio-2019
假设我们要编写一个定义具有某些类型成员或方法的匿名类的宏,然后创建该类的实例,该类通过这些方法静态地键入为结构类型等.这可以通过2.10中的宏系统实现. 0,类型成员部分非常容易:
object MacroExample extends ReflectionUtils {
import scala.language.experimental.macros
import scala.reflect.macros.Context
def foo(name: String): Any = macro foo_impl
def foo_impl(c: Context)(name: c.Expr[String]) = {
import c.universe._
val Literal(Constant(lit: String)) = name.tree
val anon = newTypeName(c.fresh)
c.Expr(Block(
ClassDef(
Modifiers(Flag.FINAL), anon, Nil, Template(
Nil, emptyValDef, List(
constructor(c.universe),
TypeDef(Modifiers(), newTypeName(lit), Nil, TypeTree(typeOf[Int]))
)
)
),
Apply(Select(New(Ident(anon)), nme.CONSTRUCTOR), Nil)
))
}
}
Run Code Online (Sandbox Code Playgroud)
(提供我方法ReflectionUtils
的便利特性在哪里constructor
.)
这个宏允许我们将匿名类的类型成员的名称指定为字符串文字:
scala> MacroExample.foo("T")
res0: AnyRef{type T = Int} = $1$$1@7da533f6
Run Code Online (Sandbox Code Playgroud)
请注意,它是适当的键入.我们可以确认一切都按预期工作:
scala> implicitly[res0.T =:= Int] …
Run Code Online (Sandbox Code Playgroud) 您遇到的最糟糕的 现实世界宏/预处理器滥用是什么(请不要设想IOCCC答案*哈哈*)?
如果它真的很有趣,请添加一个简短的片段或故事.目标是教一些东西,而不是总是告诉人们"永远不要使用宏".
ps:之前我曾经使用过宏...但是当我有一个"真正的"解决方案时,我最终会摆脱它们(即使真正的解决方案是内联的,它也会变得类似于宏).
额外:举一个例子,宏实际上比非宏解决方案更好.
相关问题: C++宏什么时候有用?
可能的重复:
当我们定义一个宏时,do(0)有什么用?
为什么在C/C++宏中有时会出现无意义的do/while和if/else语句?
做{...}而(0)有什么好处?
我已经看到一些包含在do/while(0)循环中的多行C宏,如:
#define FOO \ do { \ do_stuff_here \ do_more_stuff \ } while (0)
与使用基本块相反,以这种方式编写代码有什么好处(如果有的话):
#define FOO \ { \ do_stuff_here \ do_more_stuff \ }
我有两个宏,FOO2
并且FOO3
:
#define FOO2(x,y) ...
#define FOO3(x,y,z) ...
Run Code Online (Sandbox Code Playgroud)
我想定义一个新的宏FOO
如下:
#define FOO(x,y) FOO2(x,y)
#define FOO(x,y,z) FOO3(x,y,z)
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为宏不会在参数数量上超载.
无需修改FOO2
和FOO3
,是有一些方法来定义一个宏FOO
(使用__VA_ARGS__
或以其他方式),以获得分派的相同的效果FOO(x,y)
来FOO2
,并FOO(x,y,z)
到FOO3
?
#pragma comment
以下是什么意思?
#pragma comment(lib, "kernel32")
#pragma comment(lib, "user32")
Run Code Online (Sandbox Code Playgroud) macros ×10
c ×6
c++ ×3
boolean ×1
linux ×1
linux-kernel ×1
lisp ×1
multiline ×1
obfuscation ×1
pragma ×1
preprocessor ×1
scala ×1
scala-2.10 ×1
scala-macros ×1
visual-c++ ×1