Zenkii1337
Zenkii1337

Reputation: 40

Getting address of first passed variadic argument

#include <stdarg.h>

template<typename... Args> 
void Func(Args... args)
{
// WANT: Get the address of the first argument

// TRIED:
// va_list vargs = (char*)&(args);
// va_list vargs = (char*)&(args)...;
// va_list vargs = (char*)&(args...);

// GOT:
// 'args': parameter pack must be expanded in this context
}

I'm writing a function that accepts N arguments and does something with all of them. And for that I want to grab the address of the first element of the variadic argument list.

After that, to my knowledge the va_arg macro dereferences the pointer and shifts va_list type object with the size of assumed type

Edit: I could do probably something like this:

template<typename... Args> 
void Func(int arg1, Args... args)
{
// Code
}

But, it would result in more lines of code which I don't want.

Upvotes: 1

Views: 497

Answers (1)

Jarod42
Jarod42

Reputation: 218098

Simpler would be to split argument in signature:

template <typename T, typename... Ts> 
void Func(T arg, Ts... args)
{
// WANT: Get the address of the first argument
    T* p = &arg;
}

else

template <typename... Ts> 
void Func(Ts... args)
{
// WANT: Get the address of the first argument
    auto* p = std::get<0>(std::make_tuple(&args...));
}

Upvotes: 5

Related Questions