2016-06-16 1 views
1

VS2013에서이 기능을 사용하려고 시도합니다 (Variadic macro trickC++ preprocessor __VA_ARGS__ number of arguments 참조).C++ : Visual Studio에서 각 가변 인수를 처리하십시오.

afaik 중복 버전이 아닙니다 (다른 버전은 GCC에서만 작동합니다).

어떤 아이디어가 잘못 되었나요? 나는 ... 거의 다 해요

#define _EXPAND(x) x 
#define _VA_NARGS_IMPL(_1_, _2_, _3_, _4_, _5_, N, ...) N 
#define _VA_NARGS_IMPL2(...) _EXPAND(_VA_NARGS_IMPL(__VA_ARGS__, 4, 3, 2, 1, 0)) 
#define _PUSH_X_FRONT(...) X, __VA_ARGS__ 
/* 
Returns the number of arguments specified. 
#ifndef _MSC_VER 
    #define VA_NARGS(...) _VA_NARGS_IMPL2(X,##__VA_ARGS__) 
*/ 
#define VA_NARGS(...) _VA_NARGS_IMPL2(_PUSH_X_FRONT(__VA_ARGS__)) 

// testing is gewd 
static_assert(VA_NARGS() == 0, "VA_NARGS() failed for 0 arguments"); 
static_assert(VA_NARGS(one, two, three, four) == 4, "VA_NARGS() failed for 4 arguments"); 

#define _VARARG_IMPL2(N, Macro, ...) Macro##N(__VA_ARGS__) 
#define _VARARG_IMPL(N, Macro, ...) _VARARG_IMPL2(N, Macro, __VA_ARGS__) 
// Helper function for variadic macros with per-argument processing. 
#define VARARG(Macro, ...) _VARARG_IMPL(VA_NARGS(__VA_ARGS__), Macro, __VA_ARGS__) 


#define _Quote1(x) #x 
#define _Quote2(x, ...) #x, _Quote1(__VA_ARGS__) 
#define _Quote3(x, ...) #x, _Quote2(__VA_ARGS__) 
#define _Quote4(x, ...) #x, _Quote3(__VA_ARGS__) 
// Treat each argument as a string literal, encompassing in quotes. 
#define Quote(...) VARARG(_Quote, __VA_ARGS__) 

질문 :

constexpr char *a[] = { Quote(a, b) }; 
// WHY does the above produce {"a, b"} with msvc? 
// The following produces {"a", "b"} as expected 
constexpr char *a[] = { _Quote2(s, c) }; 

답변

0

VS2013에서 작동하는 실제 가변 인자 매크로를 만들기 어렵다. 매크로를 확장하여 매크로로 해석하여 새로운 매크로로 사용했습니다. 열쇠는 여러 수준의 매크로를 만드는 것입니다. 코드 작성은 많지만 주어진 샘플에 대해서는 작동합니다.

#define InitialMacro (argument1, argument2) \ 
DetailedMacro(argument1, argument2, argument1##_description, argument2##_description) 

#define DetailedMacro (argument1, argument2, argument3, argument4)  \ 
L#argument1  \ 
L#argument2  \ 
L#argument3  \ 
L#argument4 

여기 제시된 이상은 nedeed 매개 변수 수에 대한 모든 요구 사항을 충족시키는 데 충분한 매크로를 구현하는 것입니다. 또한 도중에 추가 항목으로 매크로를 전달/업데이트 할 수 있습니다. 기본적으로이 예제의 첫 번째 매크로는 두 번째 및 세 번째 전송 매개 변수에 _description이라는 접미사를 추가하여 매크로로 해석되는 다른 매크로를 생성하며 DetailedMacro에서 확장됩니다.

다음을 살펴볼 수도 있습니다. msvc variadic macro expansion

관련 문제