Sunteți pe pagina 1din 2

12/3/2016

c++VariadicmacroswithfunctionoverloadingStackOverflow

signup

login

tour

help

xDismiss

JointheStackOverflowCommunity
Stack Overflow is a community of 6.4 million
programmers, just like you, helping each other.
Join them it only takes a minute:
Signup

Variadicmacroswithfunctionoverloading
Ihaveafunction:
SendMsg(intx,stringy,...){/*somecode*/}

Ihaveamacro:
FOO(X,STRING,...)SendMsg(X,STRING"%s%d",##__VA_ARGS__,"xyz",123)

soIcanhavesomethinglikethis:
FOO(1000,"Notethatline%dcontaining%dwordsisinvalid",5,10);

expandedto
SendMsg(1000,"Notethatline%dcontaining%dwordsisinvalid""%s%d",5,10,"xyz",
123);

AttimesIhavesomethinglikethis:
FOO(1000,"Stringwithoutvariables");

whichshouldbeexpandedas
SendMsg(1000,"Stringwithoutvariables""%s%d","xyz",123)

Themacroworksfinesofar.
ButattimesIhavesomethinglikethis:
FOO(1000);

whichshouldbeexpandedas
SendMsg(1000,"%s"%d","xyz",123);

Butthisdoesnotwork.Igetanerrorthat"macroFOOrequires3arguments,butonly1given".
Anyideas?
c++ macros functionoverloading variableargumentlists variadicmacro
askedDec10'15at9:26

user2524261
29

cantyoupassemptystringas2ndargumenttothemacro? rahul.deshmukhpatil Dec10'15at9:32


Iwanttoavoiddoingthat.Orintroduceanothermacrothatwilldothat. user2524261 Dec10'15at9:41

Inthatcasemacrocouldonlycontainonly1stargumentasfixedone.Whilestartingfrom2ndallare

variadic.Sointhemacroitself,youcouldextract2ndargumenttomacroseparatelyandpassitas2nd
argumentoftheSendMsg.while3rdargumenttofucntionwillbe"%s%d"andthenalltheremaining
variadics+"xyz"+123.rahul.deshmukhpatilDec10'15at9:49
howdoIextractthesecondargument?andhowdoIobtainalltheremainingvariadics? user2524261

Dec10'15at9:52
pleaseseeanswer rahul.deshmukhpatil Dec10'15at10:26

http://stackoverflow.com/questions/34198003/variadicmacroswithfunctionoverloading

1/2

12/3/2016

c++VariadicmacroswithfunctionoverloadingStackOverflow

2Answers

whatyouneedtodoischangethesignatureoftheSendMsg.
SendMsg(intx,char*secondFmt,char*xyx,intno123,...)
{
//Printthestringinbufferwith##__VA_ARGS__usingsprintf.first
//argumentextractedfromwillbestringonly.otherwisesprintfwillfail
va_start(args,no123);
constchar*fmt=va_arg(args,constchar*);
bytesWrote=sprintf(buffer,fmt,args);
sprintf(buffer+bytesWrote,secondFmt,xyz,no123);
va_end(args);
}

NowFoowilllooklike
FOO(X,...)SendMsg(X,"%s%d","xyz",123,##__VA_ARGS__)

WhatIdidisIprintedmessagefrom ##__VA_ARGS__ firstandthen "%s%d" part.inthereverse


orderhowargumentsareprovidedtoSendMsg.Untested,sojusttunecode.
IfyoudonotwanttomodifytheSendMsg,seeifbelowcodeworks.
FOO(X,...){\
va_start(args,no123);\
constchar*fmt=va_arg(args,constchar*);\
SendMsg(X,fmt"%s%d","xyz",123,args);\
va_end(args);\
}\

ifnot,justwritetwomacros,:
FOO_FMT(X,STRING,...)SendMsg(X,STRING"%s%d","xyz",123,##__VA_ARGS__)
FOO(X)SendMsg(X,"%s%d","xyz",123)
editedDec10'15at11:51

answeredDec10'15at10:04

rahul.deshmukhpatil
604

20

No,IdonotwanttomodifySendMsg.AlsoIhaveaconditionalmacroexpansion.Imayormaynotpass

123. user2524261 Dec10'15at11:31


Seeifothertwomethodscouldwork.ifnot,IdonotthinkIcouldhelpyoumore. rahul.deshmukhpatil Dec

10'15at11:52

Withvariadictemplate(C++11),youmaydooverloads,somethinglike:
template<typename...Ts>
voidFOO(intx,conststd::string&format,Ts...args)
{
SendMsg(x,format+"%s%d",args...,"xyz",123);
}
voidFOO(intx)
{
SendMsg(x,"%s%d","xyz",123);
}

Demo
answeredDec10'15at13:51

Jarod42
66.9k

11

50

http://stackoverflow.com/questions/34198003/variadicmacroswithfunctionoverloading

104

2/2

S-ar putea să vă placă și