Topic: Restrictions on va_start parameter


Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Tue, 31 May 2005 16:57:23 GMT
Raw View
Old Wolf wrote:
> The C++ standard says:
>=20
>     If the parameter parmN is declared with a function, array,
>     or reference type, ..., the behaviour is undefined.
>=20
> Why shouldn't this require a diagnostic? The compiler can
> detect it easily enough, when you declare the function.

It's not having a function with such a parameter that produces undefined
behaviour, it's the use of va_start on the parameter. If you don't use
va_start, you'll be fine. So you can't detect the problem when you
declare the function, you need at least the whole function definition.

>=20
> The bit I replaced with "..." above says:
>=20
>     ... or with a type that is not compatible with the type
>     that results when passing an argument for which there is
>     no parameter ...
>=20
> What does that mean? Is it simply saying that types smaller
> than 'int' or 'double' are outlawed?

The types that can be passed as arguments for which there is no
parameters are listed in =A75.2.2/7. These are arithmetic, enumeration,
pointer, pointer to member and class type. Types smaller than int and
double are subject to standard conversions that would promote them to
int and double so they are allowed. About non-POD class types, =A75.2.2/7
says that using such a type produces undefined behaviour, but if that
also means non-PODs are "incompatible" in the sentence above is not
clear to me.

> than 'int' or 'double' are outlawed? In other words, is it
> legal for me to write:
>=20
>     std::string str_printf( std::string fmt, ... )
>     {
>         va_list ap;
>         va_start(ap, fmt);
>         char foo[1000];
>         int sz =3D std::vsnprintf(foo, sizeof foo, fmt.c_str(), ap);
>         va_end(ap);
>         return std::string(foo, sz);
>     }
>=20
> even though std::string const &fmt is illegal?

Please notice that "undefined behaviour" (or "UB") and "illegal" (aka
ill-formed), are very different concepts, defined respectively in =A71.3.=
4
and =A71.3.12. Using std::string const &fmt is not illegal: it's UB.

As I said before, I'm not sure non-POD class types are "incompatible".
If it is so, then using a std::string would invoke UB, otherwise it
would be legal. In both cases it is *not* illegal.

> What would happen if std::string had overloaded operator& ?
> Wouldn't that confuse the va_start() macro?

Confusing the va_start macro is the main reason why references are not
allowed... ;-)

Alberto

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Old Wolf" <oldwolf@inspire.net.nz>
Date: 26 May 2005 16:00:22 GMT
Raw View
The C++ standard says:

    If the parameter parmN is declared with a function, array,
    or reference type, ..., the behaviour is undefined.

Why shouldn't this require a diagnostic? The compiler can
detect it easily enough, when you declare the function.

The bit I replaced with "..." above says:

    ... or with a type that is not compatible with the type
    that results when passing an argument for which there is
    no parameter ...

What does that mean? Is it simply saying that types smaller
than 'int' or 'double' are outlawed? In other words, is it
legal for me to write:

    std::string str_printf( std::string fmt, ... )
    {
        va_list ap;
        va_start(ap, fmt);
        char foo[1000];
        int sz = std::vsnprintf(foo, sizeof foo, fmt.c_str(), ap);
        va_end(ap);
        return std::string(foo, sz);
    }

even though std::string const &fmt is illegal?

What would happen if std::string had overloaded operator& ?
Wouldn't that confuse the va_start() macro?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]