Function Arguments: Know Number w/o Null

  • Thread starter Thread starter jk22
  • Start date Start date
AI Thread Summary
Defining a function in C that accepts a variable number of arguments without a terminating null or explicit count is not possible, as traditional variadic functions do not provide a mechanism to determine the number or types of arguments. C99 and C++11 introduced variadic macros and templates, respectively, which allow for more flexible argument handling. C++11's variadic templates enable functions to accept an arbitrary number of arguments while still being able to determine their count using `sizeof...(args)`. However, caution is advised when using modern C++ features like auto and range-based for loops, especially with non-POD or complex user-defined types, as they can lead to object slicing or initialization issues. The discussion also highlights the importance of managing inheritance and constructors to mitigate slicing problems in C++.
jk22
Messages
732
Reaction score
25
Is it possible to define a function f (int **a,...)

And to know the number of arguments without writing it in a nor terminating the arguments with Null ?
 
Technology news on Phys.org
jk22 said:
Is it possible to define a function f (int **a,...)

And to know the number of arguments without writing it in a nor terminating the arguments with Null ?
As an old-style variadic function, the answer is no.

C99 and C++11 added the concept of variadic macros. I'll leave that up to someone else.

C++11 added the concept of variadic templates. For example,
Code:
#include <iostream>

template<typename... Args>
void print_pointers_to_whatever (Args... args) {
    std::cout << "print_pointers_to_whatever() received "
              << sizeof...(args) << " arguments. Values are:";
    char c[2] = {0,0};
    for (auto p : {args...}) {
        std::cout << c << ' ' << *p;
        c[0] = ',';
    } 
    std::cout << '\n';
}

int main() {
    int i1 = 1, i2 = 2, i3 = 3, i4 = 4;
    double f1 = 1.5, f2 = 2.5, f3 = 3.5;
    const char *hello = "Hello", *world = "world!";

    print_pointers_to_whatever (&i4,&i3,&i2,&i1);
    print_pointers_to_whatever (&f1,&f2,&f3);
    print_pointers_to_whatever (&hello, &world);
}

For a demo of the above, see https://ideone.com/JNe9Qv .
 
Last edited:
  • Like
Likes Silicon Waffle and jk22
Svein said:
C has a standard way of dealing with this. See https://en.wikipedia.org/wiki/Stdarg.h.
C does not have a standard way of dealing with this. The article you cited specifically states "There is no mechanism defined for determining the number or types of the unnamed arguments passed to the function. The function is simply required to know or determine this somehow, the means of which vary."
 
  • Like
Likes Silicon Waffle
D H said:
[...]
template<typename... Args>
void print_pointers_to_whatever (Args... args) {
std::cout << "print_pointers_to_whatever() received "
<< sizeof...(args) << " arguments. Values are:";
char c[2] = {0,0};
for (auto p : {args...}) {
std::cout << c << ' ' << *p;
c[0] = ',';
}
std::cout << '\n';
}
[...][/code]
I like C++11/14's latest features but auto key and this new for loop construct I think should be used with care especially when one is having to deal with non-POD or complex user-defined types without user-defined versions of copy ctor, assignment operator, with and without universal references, etc. since their objects will be sliced off or fail to get initialized in the loop.
 
D H said:
C does not have a standard way of dealing with this. The article you cited specifically states "There is no mechanism defined for determining the number or types of the unnamed arguments passed to the function. The function is simply required to know or determine this somehow, the means of which vary."
I read it somewhat differently:
  • It provides facilities for stepping through a list of function arguments of unknown number and type.
Of course it cannot specify what it does not know, it just gives you the mechanism to handle a variable number of arguments. The C++ preprocessor uses it all the time.
 
  • Like
Likes Silicon Waffle
Silicon Waffle said:
I like C++11/14's latest features but auto key and this new for loop construct I think should be used with care especially when one is having to deal with non-POD or complex user-defined types without user-defined versions of copy ctor, assignment operator, with and without universal references, etc. since their objects will be sliced off or fail to get initialized in the loop.
You can always use for (auto& element : container) in such cases.

That said, the slicing problem is ever present in C++, and it's arguably only worse with constructors, move assignment operators, universal references, etc. Inheritance isn't just a double edged sword, it's sometimes a sword with edges anywhere you look. Sometimes the best way to avoid getting cut is to be very frugal with inheritance. The slicing problem pretty much disappears if you make the constructors and assignment operators in base classes protected, only elevating them to public for the final instantiable classes. In C++11/14 you can declare those final instantiable classes as final.
 
Back
Top