在 Boost 预处理器中混合变量和整数常量

发布于 2024-09-26 17:15:18 字数 1101 浏览 4 评论 0原文

我使用 BOOST_PP 在预处理器中进行预编译计算。我专注于代码大小对我来说极其重要的应用程序。 (所以请不要说编译器应该通常这样做,我需要控制编译时执行的内容以及生成的代码)。但是,我希望能够对整数常量和变量使用相同名称的宏/函数。作为一个简单的例子,我可以让

#define TWICE(n) BOOST_PP_MUL(n,2)
//.....
// somewhere else in code
int a = TWICE(5);

This 做我想要的事情,

int a = 10;

在编译时评估。

但是,我也希望它用于

int b = 5;
int a = TWICE(b);

This should be preprocessed to

int b = 5;
int a = 5 * 2;

当然,我可以通过使用传统的宏来做到这一点,例如

#define TWICE(n) n * 2

But then it does not do what I want it do for integer Constants (在编译时评估它们)。

所以,我的问题是,是否有一个技巧可以检查参数是文字还是变量,然后使用不同的定义。即,像这样:

#define TWICE(n) BOOST_PP_IF( _IS_CONSTANT(n), \
                              BOOST_PP_MUL(n,2), \
                              n * 2 )

编辑: 所以我真正想要的是某种方法来检查某些东西是否是编译时可用的常量,因此这是 BOOST_PP_ 函数的一个很好的论据。我意识到这与大多数人对预处理器和一般编程建议的期望不同。但是编程方式没有错误,所以如果您不同意这个问题的哲学,请不要讨厌这个问题。 BOOST_PP 库存在是有原因的,这个问题也是出于同样的精神。但这可能是不可能的。

I am using BOOST_PP for to do precompile computations in the preprocessor. I am focusing on an application where code size is extremely important to me. (So please don't say the compiler should or usually does that, I need to control what is performed at compile time and what code is generated). However, I want to be able to use the same name of the macro/function for both integer constants and variables. As trivial example, I can have

#define TWICE(n) BOOST_PP_MUL(n,2)
//.....
// somewhere else in code
int a = TWICE(5);

This does what I want it to, evaluating to

int a = 10;

during compile time.

However, I also want it to be used at

int b = 5;
int a = TWICE(b);

This should be preprocessed to

int b = 5;
int a = 5 * 2;

Of course, I can do so by using the traditional macros like

#define TWICE(n) n * 2

But then it doesnt do what I want it to do for integer constants (evaluating them during compile time).

So, my question is, is there a trick to check whether the argument is a literal or a variable, and then use different definitions. i.e., something like this:

#define TWICE(n) BOOST_PP_IF( _IS_CONSTANT(n), \
                              BOOST_PP_MUL(n,2), \
                              n * 2 )

edit:
So what I am really after is some way to check if something is a constant available at compile time, and hence a good argument for the BOOST_PP_ functions. I realize that this is different from what most people expect from a preprocessor and general programming recommendations. But there is no wrong way of programming, so please don't hate on the question if you disagree with its philosophy. There is a reason the BOOST_PP library exists, and this question is in the same spirit. It might just not be possible though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半山落雨半山空 2024-10-03 17:15:18

您正在尝试做一些最好留给编译器优化的事情。

int main (void) {
  int b = 5;
  int a = b * 2;

  return a; // return it so we use a and it's not optimized away
}

gcc -O3 -s tc

 .file "t.c"
 .text
 .p2align 4,,15
.globl main
 .type main, @function
main:
.LFB0:
 .cfi_startproc
 movl $10, %eax
 ret
 .cfi_endproc
.LFE0:
 .size main, .-main
 .ident "GCC: (Debian 4.5.0-6) 4.5.1 20100617 (prerelease)"
 .section .note.GNU-stack,"",@progbits

优化编译器会优化。

编辑:我知道您不想听到编译器“应该”或“通常”这样做。然而,您想要做的事情并不是在 C 预处理器中完成的;而是应该在 C 预处理器中完成的。设计 C 语言和 C 预处理器的人将其设计为与预处理标记一起使用,因为它是基本原子。从很多方面来说,菲共都是“愚蠢的”。这并不是一件坏事(事实上,这在很多情况下使它如此有用),但归根结底,它是一个预处理器。它在解析源文件之前对其进行预处理。它在进行语义分析之前对源文件进行预处理。它在检查给定源文件的有效性之前对源文件进行预处理。我理解您不想听到这是解析器和语义分析器应该处理或通常要做的事情。然而,这就是现实情况。如果您想设计非常小的代码,那么您应该依靠编译器来完成它的工作,而不是尝试创建预处理器构造来完成这项工作。可以这样想:您的编译器投入了数千小时的工作,因此请尝试尽可能多地重用这些工作!

You're trying to do something that is better left to the optimizations of the compiler.

int main (void) {
  int b = 5;
  int a = b * 2;

  return a; // return it so we use a and it's not optimized away
}

gcc -O3 -s t.c

 .file "t.c"
 .text
 .p2align 4,,15
.globl main
 .type main, @function
main:
.LFB0:
 .cfi_startproc
 movl $10, %eax
 ret
 .cfi_endproc
.LFE0:
 .size main, .-main
 .ident "GCC: (Debian 4.5.0-6) 4.5.1 20100617 (prerelease)"
 .section .note.GNU-stack,"",@progbits

Optimizing compiler will optimize.

EDIT: I'm aware that you don't want to hear that the compiler "should" or "usually" does that. However, what you are trying to do is not something that is meant to be done in the C preprocessor; the people who designed the C language and the C preprocessor designed it to work with the preprocessing token as it's basic atom. The CPP is, in many ways, "dumb". This is not a bad thing (in fact, this is in many cases what makes it so useful), but at the end of the day, it is a preprocessor. It preprocesses source files before they are parsed. It preprocesses source files before semantic analysis occurs. It preprocesses source files before the validity of a given source file is checked. I understand that you do not want to hear that this is something that the parser and semantic analyzer should handle or usually does. However, this is the reality of the situation. If you want to design code that is incredibly small, then you should rely on your compiler to do it's job instead of trying to create preprocessor constructs to do the work. Think of it this way: thousands of hours of work went into your compiler, so try to reuse as much of that work as possible!

若能看破又如何 2024-10-03 17:15:18

然而,这不是很直接的方法:

struct operation {
    template<int N>
    struct compile {
        static const int value = N;
    };
    static int runtime(int N) { return N; }
};

operation::compile<5>::value;
operation::runtime(5);

或者

operation<5>();
operation(5);

not quite direct approach, however:

struct operation {
    template<int N>
    struct compile {
        static const int value = N;
    };
    static int runtime(int N) { return N; }
};

operation::compile<5>::value;
operation::runtime(5);

alternatively

operation<5>();
operation(5);
灯下孤影 2024-10-03 17:15:18

没有真正的机会混合这两个级别(预处理器和变量的评估)。根据我对你问题的理解,b应该是一个符号常量?

我认为您应该使用传统的变量

#define TWICE(n) ((n) * 2)

,但是您应该使用编译时常量来初始化变量,而不是使用表达式来初始化变量。我认为在编译时强制求值并在 C 中使用符号常量的唯一因素是整数枚举常量。它们被定义为 int 类型并在编译时进行评估。

enum { bInit = 5 };
int b = bInit;
enum { aInit = TWICE(bInit) };
int a = aInit; 

一般来说,您不应该过于节俭 const(对于您的 b),并使用 -S 检查生成的汇编程序。

There is no real chance of mixing the two levels (preprocessor and evaluation of variables). From what I understand from you question, b should be a symbolic constant?

I think you should use the traditional one

#define TWICE(n) ((n) * 2)

but then instead of initializing variables with expressions, you should initialize them with compile time constants. The only thing that I see to force evaluation at compile time and to have symbolic constants in C are integral enumeration constants. These are defined to have type int and are evaluated at compile time.

enum { bInit = 5 };
int b = bInit;
enum { aInit = TWICE(bInit) };
int a = aInit; 

And generally you should not be too thrifty with const (as for your b) and check the produced assembler with -S.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文