Number of arguments

  • Thread starter jk22
  • Start date
  • #1
679
20

Main Question or Discussion Point

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 ?
 

Answers and Replies

  • #2
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
683
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
  • #4
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
683
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
  • #5
156
203
[...]
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.
 
  • #6
Svein
Science Advisor
Insights Author
2,095
673
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
  • #7
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
683
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.
 

Related Threads on Number of arguments

Replies
2
Views
2K
  • Last Post
Replies
6
Views
3K
  • Last Post
Replies
4
Views
13K
  • Last Post
2
Replies
35
Views
4K
Replies
6
Views
2K
Replies
3
Views
2K
  • Last Post
Replies
6
Views
9K
  • Last Post
Replies
4
Views
3K
Replies
7
Views
11K
Top