473,479 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Temporary default argument storage duration

Hi,
According to ISO C++ (7.11/2), "An object declared without a
storage-class-specifier at block scope or declared as a function
parameter has automatic storage duration by default."
What about temporary objects used as default function arguments?
If I have:
class A{};
void g( A &b = A() ){}
what is the storage duration of the temporary?

Jul 26 '07 #1
1 1771
ma**********@comcast.net wrote:
Hi,
According to ISO C++ (7.11/2), "An object declared without a
storage-class-specifier at block scope or declared as a function
parameter has automatic storage duration by default."
What about temporary objects used as default function arguments?
Default arguments (the '= blah' syntax) is just another way to
initialise the argument (just as if you passed that expression in
when making the call).
If I have:
class A{};
void g( A &b = A() ){}
what is the storage duration of the temporary?
Your code is ill-formed. A reference to non-const cannot be
initialised with (bound to) a temporary. Change 'b' to be a ref
to 'const A', then your code would be OK.

The semantics (and the lifetime of the temporaries) is described
in [class.temporary]/5, they even have an example that is almost
like your code here. In short: it survives any call to 'g'. The
precise moment of that temporary's destruction cannot be determined
without seeing the code that calls 'g'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2435
by: smith4894 | last post by:
Hello, I have a question regarding storage locations for different data types. For example, dynamically created objects (using "new") are created on the heap. local objects ( foo() {int x;} )...
5
1673
by: White Wolf | last post by:
Hi, I would like to double check how long a temporary returned by a function lives? Suppose I have an instance of a class type C, which has a member function returning some sort of...
1
2492
by: ravi_shankar | last post by:
I know java,but I am just beginner in C.I have some confusions regarding extern storage specifier and default storage class specifier for a variable when it has file scope that is ,when it is not...
16
2302
by: Stephen Mayes | last post by:
Correct me if I am mistaken. The C standard guarantees that in a C hosted environment we can define main as: int main( int argc, char *argv ). I always assume that 'argc' is the number of...
7
2292
by: Chad Zalkin | last post by:
We are evaluating some old code that was written as part of our math library. This code uses some optimizations that I'm not sure are necessary or safe, but is a source of debate between my...
5
2044
by: Ivan Novick | last post by:
Is the string below automatic storage duration? #include <iostream> #include <string> int main(int argc, char** argv) { std::cout << std::string().size() << std::endl; return 0; }
4
3690
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
1
3736
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
6
3277
by: Troels Arvin | last post by:
Hello, I have recently run a rather large data import where the imported data i pumped through some updatable views equipped with INSTEAD OF triggers. For various reasons, the exact same data...
0
7033
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7027
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6726
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5318
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4468
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2987
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.