Adding extra arguments to a variadic argument function's va_list in c c++
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a library defined function that I cannot change, nor change the way it get's called.
The function goes a bit like this:
void originalFunction(const char *file, int line, error_type msg, const char* tag, ...);
This function will call the library defined one the following way:
void originalFunction(const char *file, int line, error_type msg, const char* tag, ...){
va_list ap;
va_start(ap, tag);
...
vExtIssueMessage(msg, ap);
...
va_end(ap);
}
What I needed to do, was to add some extra arguments to the end of the variable argument list. In this particular case, I want to append file and line to the argument list, because I cannot just call:
vExtIssueMessage(msg, ap, file, line);
As that would break the function's signature.
c++ macros variadic-functions
add a comment |
I have a library defined function that I cannot change, nor change the way it get's called.
The function goes a bit like this:
void originalFunction(const char *file, int line, error_type msg, const char* tag, ...);
This function will call the library defined one the following way:
void originalFunction(const char *file, int line, error_type msg, const char* tag, ...){
va_list ap;
va_start(ap, tag);
...
vExtIssueMessage(msg, ap);
...
va_end(ap);
}
What I needed to do, was to add some extra arguments to the end of the variable argument list. In this particular case, I want to append file and line to the argument list, because I cannot just call:
vExtIssueMessage(msg, ap, file, line);
As that would break the function's signature.
c++ macros variadic-functions
add a comment |
I have a library defined function that I cannot change, nor change the way it get's called.
The function goes a bit like this:
void originalFunction(const char *file, int line, error_type msg, const char* tag, ...);
This function will call the library defined one the following way:
void originalFunction(const char *file, int line, error_type msg, const char* tag, ...){
va_list ap;
va_start(ap, tag);
...
vExtIssueMessage(msg, ap);
...
va_end(ap);
}
What I needed to do, was to add some extra arguments to the end of the variable argument list. In this particular case, I want to append file and line to the argument list, because I cannot just call:
vExtIssueMessage(msg, ap, file, line);
As that would break the function's signature.
c++ macros variadic-functions
I have a library defined function that I cannot change, nor change the way it get's called.
The function goes a bit like this:
void originalFunction(const char *file, int line, error_type msg, const char* tag, ...);
This function will call the library defined one the following way:
void originalFunction(const char *file, int line, error_type msg, const char* tag, ...){
va_list ap;
va_start(ap, tag);
...
vExtIssueMessage(msg, ap);
...
va_end(ap);
}
What I needed to do, was to add some extra arguments to the end of the variable argument list. In this particular case, I want to append file and line to the argument list, because I cannot just call:
vExtIssueMessage(msg, ap, file, line);
As that would break the function's signature.
c++ macros variadic-functions
c++ macros variadic-functions
asked Jan 4 at 18:34
Tomas WolfTomas Wolf
1297
1297
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The solution to this was to use macros.
As you might or might not know, macros will basically do string replacement on your code during some preprocessing phase.
To solve this:
#define originalFunction(arg1, arg2, args...) intermediateFunction(arg1, arg2, args)
#define intermediateFunction(file, line, args...) originalFunction2(file, line, args, file, line)
void originalFunction2(const char *file, int line, error_type, const char*, ...);
You might notice I used 2 different macros there.
This is due to some function calls that used another macro that wouldn't get expanded, so the compiler would error out telling me I have a function of X arguments and I'm giving it X-2 arguments.
When using an intermediate macro, you are forcing the preprocessor to expand the inside macro, thus giving you the desired result.
You might also notice I used args...
and then called them as args
instead of the usual way in C++, function(arg1, arg2, ...)
and then those '...' args being named __VA_ARGS__
at the right side of the macro.
This was only to make it more eye friendly.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044334%2fadding-extra-arguments-to-a-variadic-argument-functions-va-list-in-c-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The solution to this was to use macros.
As you might or might not know, macros will basically do string replacement on your code during some preprocessing phase.
To solve this:
#define originalFunction(arg1, arg2, args...) intermediateFunction(arg1, arg2, args)
#define intermediateFunction(file, line, args...) originalFunction2(file, line, args, file, line)
void originalFunction2(const char *file, int line, error_type, const char*, ...);
You might notice I used 2 different macros there.
This is due to some function calls that used another macro that wouldn't get expanded, so the compiler would error out telling me I have a function of X arguments and I'm giving it X-2 arguments.
When using an intermediate macro, you are forcing the preprocessor to expand the inside macro, thus giving you the desired result.
You might also notice I used args...
and then called them as args
instead of the usual way in C++, function(arg1, arg2, ...)
and then those '...' args being named __VA_ARGS__
at the right side of the macro.
This was only to make it more eye friendly.
add a comment |
The solution to this was to use macros.
As you might or might not know, macros will basically do string replacement on your code during some preprocessing phase.
To solve this:
#define originalFunction(arg1, arg2, args...) intermediateFunction(arg1, arg2, args)
#define intermediateFunction(file, line, args...) originalFunction2(file, line, args, file, line)
void originalFunction2(const char *file, int line, error_type, const char*, ...);
You might notice I used 2 different macros there.
This is due to some function calls that used another macro that wouldn't get expanded, so the compiler would error out telling me I have a function of X arguments and I'm giving it X-2 arguments.
When using an intermediate macro, you are forcing the preprocessor to expand the inside macro, thus giving you the desired result.
You might also notice I used args...
and then called them as args
instead of the usual way in C++, function(arg1, arg2, ...)
and then those '...' args being named __VA_ARGS__
at the right side of the macro.
This was only to make it more eye friendly.
add a comment |
The solution to this was to use macros.
As you might or might not know, macros will basically do string replacement on your code during some preprocessing phase.
To solve this:
#define originalFunction(arg1, arg2, args...) intermediateFunction(arg1, arg2, args)
#define intermediateFunction(file, line, args...) originalFunction2(file, line, args, file, line)
void originalFunction2(const char *file, int line, error_type, const char*, ...);
You might notice I used 2 different macros there.
This is due to some function calls that used another macro that wouldn't get expanded, so the compiler would error out telling me I have a function of X arguments and I'm giving it X-2 arguments.
When using an intermediate macro, you are forcing the preprocessor to expand the inside macro, thus giving you the desired result.
You might also notice I used args...
and then called them as args
instead of the usual way in C++, function(arg1, arg2, ...)
and then those '...' args being named __VA_ARGS__
at the right side of the macro.
This was only to make it more eye friendly.
The solution to this was to use macros.
As you might or might not know, macros will basically do string replacement on your code during some preprocessing phase.
To solve this:
#define originalFunction(arg1, arg2, args...) intermediateFunction(arg1, arg2, args)
#define intermediateFunction(file, line, args...) originalFunction2(file, line, args, file, line)
void originalFunction2(const char *file, int line, error_type, const char*, ...);
You might notice I used 2 different macros there.
This is due to some function calls that used another macro that wouldn't get expanded, so the compiler would error out telling me I have a function of X arguments and I'm giving it X-2 arguments.
When using an intermediate macro, you are forcing the preprocessor to expand the inside macro, thus giving you the desired result.
You might also notice I used args...
and then called them as args
instead of the usual way in C++, function(arg1, arg2, ...)
and then those '...' args being named __VA_ARGS__
at the right side of the macro.
This was only to make it more eye friendly.
edited Jan 4 at 18:49
answered Jan 4 at 18:41
Tomas WolfTomas Wolf
1297
1297
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54044334%2fadding-extra-arguments-to-a-variadic-argument-functions-va-list-in-c-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown