C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Restrictions on va_start parameter

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Old Wolf
Guest





PostPosted: Thu May 26, 2005 4:55 pm    Post subject: Restrictions on va_start parameter Reply with quote



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 ]

Back to top
Alberto Barbati
Guest





PostPosted: Tue May 31, 2005 4:57 pm    Post subject: Re: Restrictions on va_start parameter Reply with quote



Old Wolf wrote:
Quote:
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.

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.

Quote:

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?

The types that can be passed as arguments for which there is no
parameters are listed in §5.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, §5.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.

Quote:
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?

Please notice that "undefined behaviour" (or "UB") and "illegal" (aka
ill-formed), are very different concepts, defined respectively in §1.3.4
and §1.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.

Quote:
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 ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.