次のようなことをやりたいです:
#define print_max(TYPE) \
# ifdef TYPE##_MAX \
printf("%lld\n", TYPE##_MAX); \
# endif
print_max(INT);
現在、#ifdef
関数マクロでは、 または任意のネストされたプリプロセッサ ディレクティブは許可されていないようです。何かアイデアはありますか?
更新: これは不可能のようです。実行時にチェックするハックさえも実現不可能のようです。だから、次のような方法にしようと思います:
#ifndef BLAH_MAX
# define BLAH_MAX 0
#endif
# etc... for each type I'm interested in
#define print_max(TYPE) \
if (TYPE##_MAX) \
printf("%lld\n", TYPE##_MAX);
print_max(INT);
print_max(BLAH);
ベストアンサー1
のブーストプリプロセッサ(Boost は全体としては C++ ライブラリですが、C と C++ の両方で機能します) ライブラリは、この種のタスクに役立ちます。マクロ内で #ifdef を使用する代わりに (これは許可されていません)、ファイルを複数回インクルードし、そのたびに異なるマクロを定義して、ファイルで #ifdef を使用できるようにします。
次のコードを max.c に保存すると、ファイルの先頭にある MAXES #define にリストされている各単語に対して必要な処理が実行されます。ただし、_MAX 値のいずれかが浮動小数点である場合は、プリプロセッサが浮動小数点を処理できないため、このコードは機能しません。
(Boost Processor は便利なツールですが、それほど簡単ではありません。この方法がコピー アンド ペーストよりも優れているかどうかは、あなた次第です。)
#define MAXES (SHRT)(INT)(LONG)(PATH)(DOESNT_EXIST)
#if !BOOST_PP_IS_ITERATING
/* This portion of the file (from here to #else) is the "main" file */
#include <values.h>
#include <stdio.h>
#include <boost/preprocessor.hpp>
/* Define a function print_maxes that iterates over the bottom portion of this
* file for each word in MAXES */
#define BOOST_PP_FILENAME_1 "max.c"
#define BOOST_PP_ITERATION_LIMITS (0,BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(MAXES)))
void print_maxes(void) {
#include BOOST_PP_ITERATE()
}
int main(int argc, char *argv[])
{
print_maxes();
}
#else
/* This portion of the file is evaluated multiple times, with
* BOOST_PP_ITERATION() resolving to a different number every time */
/* Use BOOST_PP_ITERATION() to look up the current word in MAXES */
#define CURRENT BOOST_PP_SEQ_ELEM(BOOST_PP_ITERATION(), MAXES)
#define CURRENT_MAX BOOST_PP_CAT(CURRENT, _MAX)
#if CURRENT_MAX
printf("The max of " BOOST_PP_STRINGIZE(CURRENT) " is %lld\n", (long long) CURRENT_MAX);
#else
printf("The max of " BOOST_PP_STRINGIZE(CURRENT) " is undefined\n");
#endif
#undef CURRENT
#undef CURRENT_MAX
#endif