Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Number of arguments

  1. Nov 28, 2015 #1
    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 ?
     
  2. jcsd
  3. Nov 28, 2015 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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 (C):
    #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: Nov 28, 2015
  4. Nov 29, 2015 #3

    Svein

    User Avatar
    Science Advisor

  5. Nov 29, 2015 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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."
     
  6. Nov 29, 2015 #5
    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.
     
  7. Nov 29, 2015 #6

    Svein

    User Avatar
    Science Advisor

    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.
     
  8. Nov 29, 2015 #7

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Number of arguments
Loading...