Thread: constructor for POD types?

  1. #16
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    From yout post #13 about quoted standard, it does not cover your point (2) below about for POD it is not initialized.

    However, can not test. I am not sure whether debugging into assembly code could help or we can see the final source code (when instantise T to int, what code looks like) to verify?

    Quote Originally Posted by CornedBee View Post
    1) This covers the behaviour for default-initialization.
    2) This covers the behaviour of the statement "T t;". Note that it does not cause default-initialization for primitives, and thus doesn't conflict with #1.

    regards,
    George

  2. #17
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The standard (draft) says a little further: "Otherwise, if no initializer is specified for an object, the object and its subobjects, if any, have an indeterminate initial value."

    That should cover T t; if T is for example int or some other POD. This should be quite common knowledge in C++, and I don't see why you wouldn't trust your compiler on this one.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #18
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    George, why are you trying to test every single statement in the C/C++ standard? That seems pretty counter-productive to me.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> does any Spec covers T t does not make zero-initialization for POD type?
    anon's post mentions that area. Please refer to it as the standard, not the "Spec". People may not know what you mean by Spec, and there is one C++ standard that everybody can refer to.

    Also, if you haven't already, please spend the $15 USD or whatever it is to get a PDF copy of the C++ standard so you can look these things up on your own. (However, if you do that please don't ask clarification questions on what the standard means, it might drive people crazy.)

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by CornedBee View Post
    You can't, because the default constructor of PODs does nothing.
    I'm confused about the terminology - Stroustrup in TC++PL (section 10.4.2, page 244) says "Built-in types also have default constructors (Section 6.2.8)." So, is int() (for example), which does zero initialization, a default constructor, or not?

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It is. As I said, my earlier wording was inaccurate.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #22
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks anon,


    Quote Originally Posted by anon View Post
    The standard (draft) says a little further: "Otherwise, if no initializer is specified for an object, the object and its subobjects, if any, have an indeterminate initial value."

    That should cover T t; if T is for example int or some other POD. This should be quite common knowledge in C++, and I don't see why you wouldn't trust your compiler on this one.
    Not sure whether you mean for POD, it is zero-initialized?


    Thanks CornedBee,


    Sorry that I am lost in the context of discussion. Do you mean for POD types, it is zero-initialized?

    Quote Originally Posted by CornedBee View Post
    It is. As I said, my earlier wording was inaccurate.

    regards,
    George

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Not sure whether you mean for POD, it is zero-initialized?
    No, that's not what anon means.

    >> Do you mean for POD types, it is zero-initialized?
    No, that's not what CornedBee means.

    >> Sorry that I am lost in the context of discussion.
    Please give more context for your question if you truly want it answered. Many different cases has been covered in this thread. If you have a question about one of the cases, find where it is mentioned and ask if you need clarification. I think your answers are all here.

  9. #24
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Daved,


    I should say sorry to make this thread a little in-convenient to read. Too many people and cluttered.

    Let me repeat and simplify my question, suppose we are writing a template (template function or template class), for statement like,

    (suppose T is type parameter of a template, and T is a POD type)

    1. T t;
    2. T();

    In both cases, for the initialization of new object, will be zero-initialized, default-initialized or not initialized by standard?

    Thanks and let me know if my question is not clear and I can enhance it. :-)

    Quote Originally Posted by Daved View Post
    >> Not sure whether you mean for POD, it is zero-initialized?
    No, that's not what anon means.

    >> Do you mean for POD types, it is zero-initialized?
    No, that's not what CornedBee means.

    >> Sorry that I am lost in the context of discussion.
    Please give more context for your question if you truly want it answered. Many different cases has been covered in this thread. If you have a question about one of the cases, find where it is mentioned and ask if you need clarification. I think your answers are all here.

    regards,
    George

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That question has been answered in this thread. To summarize:

    (suppose T is type parameter of a template, and T is a POD type)
    1. T t; will leave t uninitialized.
    2. T(); will zero-initialize.

  11. #26
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    #include <iostream>
    
    using namespace std;
    
    template <typename T>
    struct Test
    {
    	Test()
    	:	m_Zero( T() ) {}
    
    	T	m_Zero;
    };
    
    int main()
    {
    	int i;
    	int j = int();
    	Test<int> test;
    
    	cout << "i = " << i << endl
    	     << "j = " << j << endl
    	     << "test.m_Zero = " << test.m_Zero << endl;
    
    	return 0;
    }
    Output is this:
    Code:
    i = 4206628
    j = 0
    test.m_Zero = 0

  12. #27
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for your great comments and summary, Daved!


    English is not my native language, so sometimes confused.

    Quote Originally Posted by Daved View Post
    That question has been answered in this thread. To summarize:

    (suppose T is type parameter of a template, and T is a POD type)
    1. T t; will leave t uninitialized.
    2. T(); will zero-initialize.

    Thanks cpjust,


    Your reply is great, conforming to Daved's comments in post #25.

    Quote Originally Posted by cpjust View Post
    Code:
    #include <iostream>
    
    using namespace std;
    
    template <typename T>
    struct Test
    {
    	Test()
    	:	m_Zero( T() ) {}
    
    	T	m_Zero;
    };
    
    int main()
    {
    	int i;
    	int j = int();
    	Test<int> test;
    
    	cout << "i = " << i << endl
    	     << "j = " << j << endl
    	     << "test.m_Zero = " << test.m_Zero << endl;
    
    	return 0;
    }
    Output is this:
    Code:
    i = 4206628
    j = 0
    test.m_Zero = 0

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  2. Generalising methods to handle multiple weapon types
    By Swarvy in forum Game Programming
    Replies: 2
    Last Post: 05-22-2009, 02:52 AM
  3. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  4. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM