IT박스

GCC의 ## __ VA_ARGS__ 트릭에 대한 표준 대안?

itboxs 2020. 6. 22. 08:09
반응형

GCC의 ## __ VA_ARGS__ 트릭에 대한 표준 대안?


잘 알려진 문제 C99에서 가변 인자 매크로 빈 인수와 함께.

예:

#define FOO(...)       printf(__VA_ARGS__)
#define BAR(fmt, ...)  printf(fmt, __VA_ARGS__)

FOO("this works fine");
BAR("this breaks!");

BAR()의 사용은 C99 표준에 따라 실제로 다음 같이 확장되기 때문에 올바르지 않습니다.

printf("this breaks!",);

후행 쉼표에 유의하십시오.

일부 컴파일러 (예 : Visual Studio 2010)는 후행 쉼표를 자동으로 제거합니다. 다른 컴파일러 (예 : GCC) ##는 다음 __VA_ARGS__과 같이 앞에 배치 지원합니다 .

#define BAR(fmt, ...)  printf(fmt, ##__VA_ARGS__)

그러나이 동작을 수행하는 표준 호환 방법이 있습니까? 여러 매크로를 사용하고 있습니까?

현재 ##버전은 (적어도 내 플랫폼에서는) 상당히 잘 지원되는 것처럼 보이지만 실제로 표준 호환 솔루션을 사용하고 싶습니다.

선제 적 : 작은 함수 만 작성할 수 있다는 것을 알고 있습니다. 매크로를 사용 하여이 작업을 수행하려고합니다.

편집 : 다음은 BAR ()을 사용하려는 이유의 예입니다 (단순하지만).

#define BAR(fmt, ...)  printf(fmt "\n", ##__VA_ARGS__)

BAR("here is a log message");
BAR("here is a log message with a param: %d", 42);

fmt항상 큰 따옴표로 묶인 C 문자열 이라고 가정하면 BAR () 로깅 문에 줄 바꿈이 자동으로 추가됩니다 . 개행을 별도의 printf ()로 인쇄하지 않습니다. 로깅이 라인 버퍼링되고 여러 소스에서 비동기 적으로 오는 경우에 유리합니다.


이 질문에 대한 Richard Hansen의 답변에,##__VA_ARGS__ 설명 된대로 가변성 매크로에 전달할 수있는 인수 수에 대해 하드 코딩 된 상한값을 기꺼이 받아들이려는 경우 GCC 확장을 사용하지 않을 수 있습니다 . 그러나 그러한 제한을 원치 않으면 C99 지정 전 처리기 기능 만 사용하는 것이 불가능합니다. 언어에 대한 확장명을 사용해야합니다. clang과 icc는이 GCC 확장을 채택했지만 MSVC는 그렇지 않습니다.

2001 년에 나는 문서 N976 에 표준화를위한 GCC 확장 (및 __VA_ARGS__나머지 매개 변수가 아닌 다른 이름을 사용할 수있게하는 관련 확장 )을 작성 했지만위원회로부터 아무런 응답도받지 못했다. 나는 누군가 그것을 읽는지조차 모른다. 2016 년에 그것은 N2023 에서 다시 제안 되었으며 , 그 제안이 어떻게 우리에게 의견에 알려 질지 알고있는 사람에게 권장합니다.


사용할 수있는 인수 계산 기법이 있습니다.

다음은 BAR()jwd의 질문에서 두 번째 를 구현하는 표준 호환 방법 중 하나입니다 .

#include <stdio.h>

#define BAR(...) printf(FIRST(__VA_ARGS__) "\n" REST(__VA_ARGS__))

/* expands to the first argument */
#define FIRST(...) FIRST_HELPER(__VA_ARGS__, throwaway)
#define FIRST_HELPER(first, ...) first

/*
 * if there's only one argument, expands to nothing.  if there is more
 * than one argument, expands to a comma followed by everything but
 * the first argument.  only supports up to 9 arguments but can be
 * trivially expanded.
 */
#define REST(...) REST_HELPER(NUM(__VA_ARGS__), __VA_ARGS__)
#define REST_HELPER(qty, ...) REST_HELPER2(qty, __VA_ARGS__)
#define REST_HELPER2(qty, ...) REST_HELPER_##qty(__VA_ARGS__)
#define REST_HELPER_ONE(first)
#define REST_HELPER_TWOORMORE(first, ...) , __VA_ARGS__
#define NUM(...) \
    SELECT_10TH(__VA_ARGS__, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE,\
                TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, ONE, throwaway)
#define SELECT_10TH(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, ...) a10

int
main(int argc, char *argv[])
{
    BAR("first test");
    BAR("second test: %s", "a string");
    return 0;
}

이 같은 트릭이 사용됩니다 :

설명

전략은 __VA_ARGS__첫 번째 인수와 나머지 인수 (있는 경우)로 분리하는 것입니다. 이를 통해 첫 번째 인수 다음에 두 번째 인수 (있는 경우) 앞에 항목을 삽입 할 수 있습니다.

FIRST()

이 매크로는 단순히 첫 번째 인수로 확장되어 나머지는 버립니다.

구현은 간단합니다. throwaway인수 FIRST_HELPER()는 두 개 이상의 인수 얻도록 보장 하는데, ...하나 이상의 인수가 필요하기 때문에 필요합니다. 하나의 인수로 다음과 같이 확장됩니다.

  1. FIRST(firstarg)
  2. FIRST_HELPER(firstarg, throwaway)
  3. firstarg

둘 이상인 경우 다음과 같이 확장됩니다.

  1. FIRST(firstarg, secondarg, thirdarg)
  2. FIRST_HELPER(firstarg, secondarg, thirdarg, throwaway)
  3. firstarg

REST()

이 매크로는 첫 번째 인수를 제외하고 모든 인수로 확장됩니다 (두 개 이상의 인수가있는 경우 첫 번째 인수 다음에 쉼표 포함).

이 매크로의 구현은 훨씬 더 복잡합니다. 일반적인 전략은 인수의 수 (하나 이상)를 세고 (하나의 인수 REST_HELPER_ONE()만 제공된 경우) 또는 REST_HELPER_TWOORMORE()(두 개 이상의 인수가 제공된 경우 ) 로 확장하는 것 입니다. REST_HELPER_ONE()첫 번째 이후에는 인수가 없으므로 나머지 인수는 빈 세트입니다. REST_HELPER_TWOORMORE()또한 간단합니다. 첫 번째 인수를 제외한 모든 항목이 쉼표로 확장됩니다.

인수는 NUM()매크로를 사용하여 계산됩니다 . 이 매크로 ONE는 하나의 인수 만 제공 TWOORMORE하면 2 개에서 9 개의 인수가 제공되면 확장되고 10 개 이상의 인수가 제공되면 분리됩니다 (10 번째 인수로 확장).

NUM()매크로를 사용하는 SELECT_10TH()인수의 수를 결정하는 매크로를. 이름에서 알 수 있듯이 SELECT_10TH()단순히 10 번째 인수로 확장됩니다. 줄임표로 인해 SELECT_10TH()최소 11 개의 인수를 전달해야합니다 (표준에서는 줄임표에 대해 하나 이상의 인수가 있어야한다고 말합니다). 이것이 마지막 인수로 NUM()전달 throwaway되는 이유입니다 (한 개의 인수를 전달하지 않으면 NUM()10 개의 인수 만 전달되어 SELECT_10TH()표준을 위반 함).

하나의 선택 REST_HELPER_ONE()또는 REST_HELPER_TWOORMORE()연결하여 이루어집니다 REST_HELPER_의 확장과 NUM(__VA_ARGS__)에서 REST_HELPER2(). 의 목적 은와 연결하기 전에 완전히 확장 REST_HELPER()되도록하는 NUM(__VA_ARGS__)것입니다 REST_HELPER_.

하나의 인수로 확장하면 다음과 같습니다.

  1. REST(firstarg)
  2. REST_HELPER(NUM(firstarg), firstarg)
  3. REST_HELPER2(SELECT_10TH(firstarg, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, ONE, throwaway), firstarg)
  4. REST_HELPER2(ONE, firstarg)
  5. REST_HELPER_ONE(firstarg)
  6. (빈)

두 개 이상의 인수를 사용한 확장은 다음과 같습니다.

  1. REST(firstarg, secondarg, thirdarg)
  2. REST_HELPER(NUM(firstarg, secondarg, thirdarg), firstarg, secondarg, thirdarg)
  3. REST_HELPER2(SELECT_10TH(firstarg, secondarg, thirdarg, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, TWOORMORE, ONE, throwaway), firstarg, secondarg, thirdarg)
  4. REST_HELPER2(TWOORMORE, firstarg, secondarg, thirdarg)
  5. REST_HELPER_TWOORMORE(firstarg, secondarg, thirdarg)
  6. , secondarg, thirdarg

일반적인 해결책은 아니지만 printf의 경우 다음과 같은 줄 바꿈을 추가 할 수 있습니다.

#define BAR_HELPER(fmt, ...) printf(fmt "\n%s", __VA_ARGS__)
#define BAR(...) BAR_HELPER(__VA_ARGS__, "")

형식 문자열에서 참조되지 않은 추가 인수를 무시한다고 생각합니다. 따라서 아마도 다음과 같이 벗어날 수도 있습니다.

#define BAR_HELPER(fmt, ...) printf(fmt "\n", __VA_ARGS__)
#define BAR(...) BAR_HELPER(__VA_ARGS__, 0)

표준 방법이 없으면 C99가 승인되었다고 믿을 수 없습니다. AFAICT 문제는 C ++ 11에도 존재합니다.


Boost.Preprocessor 와 같은 것을 사용 하여이 특정 사례를 처리하는 방법이 있습니다 . BOOST_PP_VARIADIC_SIZE사용 하여 인수 목록의 크기를 확인한 다음 조건부로 다른 매크로로 확장 할 수 있습니다. 이것의 한 가지 단점은 0과 1 인수를 구별 할 수 없으며 다음을 고려하면 그 이유가 분명해진다는 것입니다.

BOOST_PP_VARIADIC_SIZE()      // expands to 1
BOOST_PP_VARIADIC_SIZE(,)     // expands to 2
BOOST_PP_VARIADIC_SIZE(,,)    // expands to 3
BOOST_PP_VARIADIC_SIZE(a)     // expands to 1
BOOST_PP_VARIADIC_SIZE(a,)    // expands to 2
BOOST_PP_VARIADIC_SIZE(,b)    // expands to 2
BOOST_PP_VARIADIC_SIZE(a,b)   // expands to 2
BOOST_PP_VARIADIC_SIZE(a, ,c) // expands to 3

빈 매크로 인수 목록은 실제로 비어있는 하나의 인수로 구성됩니다.

이 경우, 원하는 매크로에는 항상 적어도 하나의 인수가 있으므로 두 개의 "오버로드"매크로로 구현할 수 있습니다.

#define BAR_0(fmt) printf(fmt "\n")
#define BAR_1(fmt, ...) printf(fmt "\n", __VA_ARGS__)

그런 다음 다른 매크로를 사용하여 다음과 같이 전환하십시오.

#define BAR(...) \
    BOOST_PP_CAT(BAR_, BOOST_PP_GREATER(
        BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), 1))(__VA_ARGS__) \
    /**/

또는

#define BAR(...) BOOST_PP_IIF( \
    BOOST_PP_GREATER(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), 1), \
        BAR_1, BAR_0)(__VA_ARGS__) \
    /**/

더 읽기 쉬운 것을 찾으십시오 (인수의 수에 매크로를 오버로드하는 일반적인 형태를 제공하기 때문에 첫 번째 방법을 선호합니다).

변수 인수 목록에 액세스하고 변경하여 단일 매크로를 사용하여이 작업을 수행 할 수도 있지만 읽기 어렵고이 문제에 매우 구체적입니다.

#define BAR(...) printf( \
    BOOST_PP_VARIADIC_ELEM(0, __VA_ARGS__) "\n" \
    BOOST_PP_COMMA_IF( \
        BOOST_PP_GREATER(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), 1)) \
    BOOST_PP_ARRAY_ENUM(BOOST_PP_ARRAY_POP_FRONT( \
        BOOST_PP_VARIADIC_TO_ARRAY(__VA_ARGS__)))) \
    /**/

또한 왜 BOOST_PP_ARRAY_ENUM_TRAILING이 없습니까? 이 솔루션을 훨씬 덜 끔찍하게 만들 것입니다.

편집 : 좋아, 여기 BOOST_PP_ARRAY_ENUM_TRAILING 및 그것을 사용하는 버전이 있습니다 (이것은 내가 가장 좋아하는 솔루션입니다).

#define BOOST_PP_ARRAY_ENUM_TRAILING(array) \
    BOOST_PP_COMMA_IF(BOOST_PP_ARRAY_SIZE(array)) BOOST_PP_ARRAY_ENUM(array) \
    /**/

#define BAR(...) printf( \
    BOOST_PP_VARIADIC_ELEM(0, __VA_ARGS__) "\n" \
    BOOST_PP_ARRAY_ENUM_TRAILING(BOOST_PP_ARRAY_POP_FRONT( \
        BOOST_PP_VARIADIC_TO_ARRAY(__VA_ARGS__)))) \
    /**/

최근 비슷한 문제가 발생했으며 해결책이 있다고 생각합니다.

핵심 아이디어는 매크로 NUM_ARGS작성 하여 가변성 매크로가 제공되는 인수의 수를 세는 방법이 있다는 것 입니다. 당신의 변형을 사용할 수있는 NUM_ARGS빌드로 NUM_ARGS_CEILING2가변 인자 매크로가 1 개 인자 또는 2 또는-이상의 인수가 주어집니다 여부를 알 수있다. 그런 다음 Bar매크로 를 작성하여 인수를 사용 NUM_ARGS_CEILING2하여 CONCAT두 개의 도우미 매크로 중 하나에 인수를 보낼 수 있습니다. 하나는 정확히 1 개의 인수를 예상하고 다른 하나는 1보다 큰 인수를 기대합니다.

여기 매크로 작성이 트릭을 사용하는 예입니다 UNIMPLEMENTED매우 유사하다 BAR:

1 단계:

/** 
 * A variadic macro which counts the number of arguments which it is
 * passed. Or, more precisely, it counts the number of commas which it is
 * passed, plus one.
 *
 * Danger: It can't count higher than 20. If it's given 0 arguments, then it
 * will evaluate to 1, rather than to 0.
 */

#define NUM_ARGS(...)                                                   \
    NUM_ARGS_COUNTER(__VA_ARGS__, 20, 19, 18, 17, 16, 15, 14, 13,       \
                     12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)    

#define NUM_ARGS_COUNTER(a1, a2, a3, a4, a5, a6, a7,        \
                         a8, a9, a10, a11, a12, a13,        \
                         a14, a15, a16, a17, a18, a19, a20, \
                         N, ...)                            \
    N

1.5 단계 :

/*
 * A variant of NUM_ARGS that evaluates to 1 if given 1 or 0 args, or
 * evaluates to 2 if given more than 1 arg. Behavior is nasty and undefined if
 * it's given more than 20 args.
 */

#define NUM_ARGS_CEIL2(...)                                           \
    NUM_ARGS_COUNTER(__VA_ARGS__, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \
                     2, 2, 2, 2, 2, 2, 2, 1)

2 단계:

#define _UNIMPLEMENTED1(msg)                                        \
    log("My creator has forsaken me. %s:%s:%d." msg, __FILE__,      \
        __func__, __LINE__)

#define _UNIMPLEMENTED2(msg, ...)                                   \
    log("My creator has forsaken me. %s:%s:%d." msg, __FILE__,      \
        __func__, __LINE__, __VA_ARGS__)

3 단계 :

#define UNIMPLEMENTED(...)                                              \
    CONCAT(_UNIMPLEMENTED, NUM_ARGS_CEIL2(__VA_ARGS__))(__VA_ARGS__)

Where CONCAT is implemented in the usual way. As a quick hint, if the above seems confusing: the goal of CONCAT there is to expand to another macro "call".

Note that NUM_ARGS itself isn't used. I just included it to illustrate the basic trick here. See Jens Gustedt's P99 blog for a nice treatment of it.

Two notes:

  • NUM_ARGS is limited in the number of arguments that it handles. Mine can only handle up to 20, although the number is totally arbitrary.

  • NUM_ARGS, as shown, has a pitfall in that it returns 1 when given 0 arguments. The gist of it is that NUM_ARGS is technically counting [commas + 1], and not args. In this particular case, it actually works to our advantage. _UNIMPLEMENTED1 will handle an empty token just fine and it saves us from having to write _UNIMPLEMENTED0. Gustedt has a workaround for that as well, although I haven't used it and I'm not sure if it would work for what we're doing here.


A very simple macro I'm using for debug printing:

#define __DBG_INT(fmt, ...) printf(fmt "%s", __VA_ARGS__);
#define DBG(...) __DBG_INT(__VA_ARGS__, "\n")

int main() {
        DBG("No warning here");
        DBG("and we can add as many arguments as needed. %s", "nice!");
        return 0;
}

No matter how many arguments are passed to DBG there are no c99 warning.

The trick is __DBG_INT adding a dummy param so ... will always have at least one argument and c99 is satisfied.


This is the simplified version that I use. It is based upon the great techniques of the other answers here, so many props to them:

#define _SELECT(PREFIX,_5,_4,_3,_2,_1,SUFFIX,...) PREFIX ## _ ## SUFFIX

#define _BAR_1(fmt)      printf(fmt "\n")
#define _BAR_N(fmt, ...) printf(fmt "\n", __VA_ARGS__);
#define BAR(...) _SELECT(_BAR,__VA_ARGS__,N,N,N,N,1)(__VA_ARGS__)

int main(int argc, char *argv[]) {
    BAR("here is a log message");
    BAR("here is a log message with a param: %d", 42);
    return 0;
}

That's it.

As with other solutions this is limited to the number of arguments the macro. To support more, add more parameters to _SELECT, and more N arguments. The argument names count down (instead of up) to serve as a reminder that the count-based SUFFIX argument is provided in reverse order.

This solution treats 0 arguments as though it is 1 argument. So BAR() nominally "works", because it expands to _SELECT(_BAR,,N,N,N,N,1)(), which expands to _BAR_1()(), which expands to printf("\n").

If you want, you can get creative with the use of _SELECT and provide different macros for different number of arguments. For example, here we have a LOG macro that takes a 'level' argument before the format. If format is missing, it logs "(no message)", if there is just 1 argument, it will log it through "%s", otherwise it will treat the format argument as a printf format string for the remaining arguments.

#define _LOG_1(lvl)          printf("[%s] (no message)\n", #lvl)
#define _LOG_2(lvl,fmt)      printf("[%s] %s\n", #lvl, fmt)
#define _LOG_N(lvl,fmt, ...) printf("[%s] " fmt "\n", #lvl, __VA_ARGS__)
#define LOG(...) _SELECT(_LOG,__VA_ARGS__,N,N,N,2,1)(__VA_ARGS__)

int main(int argc, char *argv[]) {
    LOG(INFO);
    LOG(DEBUG, "here is a log message");
    LOG(WARN, "here is a log message with param: %d", 42);
    return 0;
}
/* outputs:
[INFO] (no message)
[DEBUG] here is a log message
[WARN] here is a log message with param: 42
*/

In your situation (at least 1 argument present, never 0), you can define BAR as BAR(...), use Jens Gustedt's HAS_COMMA(...) to detect a comma and then dispatch to BAR0(Fmt) or BAR1(Fmt,...) accordingly.

This:

#define HAS_COMMA(...) HAS_COMMA_16__(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0)
#define HAS_COMMA_16__(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, ...) _15
#define CAT_(X,Y) X##Y
#define CAT(X,Y) CAT_(X,Y)
#define BAR(.../*All*/) CAT(BAR,HAS_COMMA(__VA_ARGS__))(__VA_ARGS__)
#define BAR0(X) printf(X "\n")
#define BAR1(X,...) printf(X "\n",__VA_ARGS__)


#include <stdio.h>
int main()
{
    BAR("here is a log message");
    BAR("here is a log message with a param: %d", 42);
}

compiles with -pedantic without a warning.


C (gcc), 762 bytes

#define EMPTYFIRST(x,...) A x (B)
#define A(x) x()
#define B() ,

#define EMPTY(...) C(EMPTYFIRST(__VA_ARGS__) SINGLE(__VA_ARGS__))
#define C(...) D(__VA_ARGS__)
#define D(x,...) __VA_ARGS__

#define SINGLE(...) E(__VA_ARGS__, B)
#define E(x,y,...) C(y(),)

#define NONEMPTY(...) F(EMPTY(__VA_ARGS__) D, B)
#define F(...) G(__VA_ARGS__)
#define G(x,y,...) y()

#define STRINGIFY(...) STRINGIFY2(__VA_ARGS__)
#define STRINGIFY2(...) #__VA_ARGS__

#define BAR(fmt, ...) printf(fmt "\n" NONEMPTY(__VA_ARGS__) __VA_ARGS__)

int main() {
    puts(STRINGIFY(NONEMPTY()));
    puts(STRINGIFY(NONEMPTY(1)));
    puts(STRINGIFY(NONEMPTY(,2)));
    puts(STRINGIFY(NONEMPTY(1,2)));

    BAR("here is a log message");
    BAR("here is a log message with a param: %d", 42);
}

Try it online!

Assumes:

  • No arg contain comma or bracket
  • No arg contain A~G (can rename to hard_collide ones)

The standard solution is to use FOO instead of BAR. There are a few weird cases of argument reordering it probably can't do for you (though I bet someone can come up with clever hacks to disassemble and reassemble __VA_ARGS__ conditionally based on the number of arguments in it!) but in general using FOO "usually" just works.

참고URL : https://stackoverflow.com/questions/5588855/standard-alternative-to-gccs-va-args-trick

반응형