Objects in var. length arguments (?)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alvaro Segura

    Objects in var. length arguments (?)

    Hello,

    I have this doubt that I find no answer for:

    Can an object or a reference to an object be passed as an argument to a
    variable length arguments function? And if so, how does it work?

    // suppose we have:
    void f(int x, ...);
    class A {};

    // and we do:
    A a;
    f(3, a); // is this OK?

    I recently found that MSVC++ was not complaining when I accidentally did
    something like that, but I thought it was wrong. What about the standard?




  • Michael Kochetkov

    #2
    Re: Objects in var. length arguments (?)


    "Alvaro Segura" <asegura.NO_SPA M_@vicomtech.es > wrote in message
    news:bod9vo$902 $2@unbe.sarenet .es...[color=blue]
    > Hello,
    >
    > I have this doubt that I find no answer for:
    >
    > Can an object or a reference to an object be passed as an argument to a
    > variable length arguments function? And if so, how does it work?
    >
    > // suppose we have:
    > void f(int x, ...);
    > class A {};
    >
    > // and we do:
    > A a;
    > f(3, a); // is this OK?
    >
    > I recently found that MSVC++ was not complaining when I accidentally did
    > something like that, but I thought it was wrong. What about the standard?[/color]
    Your code is OK. It is the f's problem to find A object on the stack. The
    common solution is to pass a kind of a format information to f.

    --
    Michael Kochetkov.


    Comment

    • Ron Natalie

      #3
      Re: Objects in var. length arguments (?)


      "Michael Kochetkov" <Michael.Kochet kov@synartra.co mmmm> wrote in message news:3faa6e4a@n ews.telekom.ru. ..
      [color=blue]
      > Your code is OK. It is the f's problem to find A object on the stack. The
      > common solution is to pass a kind of a format information to f.[/color]

      In this case, since A is of POD type, it is ok.
      Had the class A been been a little bit more elaborate (so as to make it non-POD)
      it would have been undefined behavior.


      Comment

      • Victor Bazarov

        #4
        Re: Objects in var. length arguments (?)

        "Alvaro Segura" <asegura.NO_SPA M_@vicomtech.es > wrote...[color=blue]
        > I have this doubt that I find no answer for:
        >
        > Can an object or a reference to an object be passed as an argument to a
        > variable length arguments function? And if so, how does it work?[/color]

        An object can, but only if the object is of POD type. A reference
        probably cannot.
        [color=blue]
        > // suppose we have:
        > void f(int x, ...);
        > class A {};
        >
        > // and we do:
        > A a;
        > f(3, a); // is this OK?[/color]

        Yes, class A is a POD.
        [color=blue]
        > I recently found that MSVC++ was not complaining when I accidentally did
        > something like that, but I thought it was wrong. What about the standard?[/color]

        The Standard says that if it's not POD, the behaviour is undefined.
        See 5.2.2/7.

        Victor


        Comment

        Working...