0

In C, I can do this to have an unspecified number of arguments in a function:

#include <elf.h>
#include <stddef.h>
#include <stdlib.h>

extern char **__environ;

int __libc_start_main
(
int (*main)(),
int argc,
char **argv
)
{
    int ret;
    Elf32_auxv_t *auxv;
    size_t aux[38];

    /* ... */

    exit(main(argc, argv, __environ, aux));
}

However, when doing this in C++, the compiler emits this error:

test.c: In function ‘int __libc_start_main(int (*)(), int, char**)’:
test.c:21:45: error: too many arguments to function
         exit(main(argc, argv, __environ, aux));
                                             ^

How do I do this in C++?

I understand that the C/C++ standards don't allow this, but I'm currently writing an implementation of the standard C library.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/195384/discussion-on-question-by-jl2210-how-do-i-declare-the-main-entry-point-of-a-pr). – Samuel Liew Jun 22 '19 at 01:25

2 Answers2

3

The short answer is: You don't.

In C++ all functions have a prototype; there is no such thing as an "unspecified number of arguments".

If you want to call main as main(argc, argv, __environ, aux), you need to declare it as int (*main)(int, char **, char **, void *) or similar.

melpomene
  • 84,125
  • 8
  • 85
  • 148
2

Try either:

void foo(...);

or

template <typename ... ARGS> void foo(ARGS && ... args) { ... body }

First option is the same as void foo() (little known C language fact). First option requires some sort of additional argument (for example printf(char *, ...), where first argument allows function to detect, how to parse following arguments).

Second option requires you to commit to a function body somewhere in a header.

Radosław Cybulski
  • 2,952
  • 10
  • 21
  • 5
    `void foo(...)` is not legal C (per the grammar in C 2018 6.7.6, the *parameter-type-list* must have at least one parameter), and, if it were, it would not be equivalent to `void foo()`, due to rules about compatible types, at least. – Eric Postpischil Jun 21 '19 at 13:06