const object declaration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    #1

    const object declaration

    Consider the following program:

    #include <iostream>

    using namespace std;

    class X
    {
    public:
    int value() const { return val; }
    int val;
    };

    int main()
    {
    const X obj;

    return 0;
    }

    When this program is compiled with g++ as
    g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

    the following compilation error is generated for the line const X obj;

    error: uninitialized const `obj'

    However VC++ 2005 Express Edition gives only the following warning(no
    error):

    warning: 'obj' : 'const' automatic data initialized with compiler
    generated default constructor produces unreliable results

    Which is the correct behaviour as per the standard ?

    Kindly clarify.

    Also please let me know how to set the compilation flags in VC++ 2005
    Express Edition IDE similar to the command line g++ compilation flags
    as mentioned above.

    Thanks
    V.Subramanian
  • benben

    #2
    Re: const object declaration

    Which is the correct behaviour as per the standard ?

    I don't know. But my bet is if you define an empty constructor your
    problem should go away.
    >
    Kindly clarify.
    >
    Also please let me know how to set the compilation flags in VC++ 2005
    Express Edition IDE similar to the command line g++ compilation flags
    as mentioned above.
    That, im afraid, is better asked in the gcc group.

    Ben

    Comment

    • Victor Bazarov

      #3
      Re: const object declaration

      benben wrote:
      >[..]
      >Also please let me know how to set the compilation flags in VC++ 2005
      >Express Edition IDE similar to the command line g++ compilation flags
      >as mentioned above.
      >
      That, im afraid, is better asked in the gcc group.
      Or in the VC++ newsgroup.

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


      Comment

      • Abhishek Padmanabh

        #4
        Re: const object declaration

        On Dec 3, 7:54 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        benben wrote:
        [..]
        Also please let me know how to set the compilation flags in VC++ 2005
        Express Edition IDE similar to the command line g++ compilation flags
        as mentioned above.
        >
        That, im afraid, is better asked in the gcc group.
        >
        Or in the VC++ newsgroup.
        >
        V
        I asked this once on clc++m and had not got a convincing answer. It is
        mandated by the standard that a user-defined constructor must be
        provided. It is needed even though there are no members in the class.
        It just is as it is. I am not aware of the rationale behind this
        though.

        In case the class has a POD type, it would remain uninitialized
        (indeterminate value) when default constructor (by the compiler
        generated default constructor). In case of non-POD members, they would
        have been default initialized. An indeterminate POD type's value is
        rather useless but for non-POD types, it might make sense. I don't
        know about the rationale but may be it is based on something along
        these lines, to avoid complexity. I don't know for sure. May be some
        guru will enlighten us! :)

        Comment

        • Victor Bazarov

          #5
          Re: const object declaration

          Abhishek Padmanabh wrote:
          On Dec 3, 7:54 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
          >benben wrote:
          >>>[..]
          >>>Also please let me know how to set the compilation flags in VC++
          >>>2005 Express Edition IDE similar to the command line g++
          >>>compilatio n flags as mentioned above.
          >>
          >>That, im afraid, is better asked in the gcc group.
          >>
          >Or in the VC++ newsgroup.
          >>
          >V
          >
          I asked this once on clc++m and had not got a convincing answer. [..]
          As you may have noticed, I removed all mention of the C++ problem but
          left in the "compilatio n flags" request. That (and only that) is what
          I am suggesting asking about in the VC++ newsgroup.

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


          Comment

          • Abhishek Padmanabh

            #6
            Re: const object declaration

            On Dec 3, 8:30 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
            Abhishek Padmanabh wrote:
            On Dec 3, 7:54 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
            benben wrote:
            >>[..]
            >>Also please let me know how to set the compilation flags in VC++
            >>2005 Express Edition IDE similar to the command line g++
            >>compilation flags as mentioned above.
            >
            >That, im afraid, is better asked in the gcc group.
            >
            Or in the VC++ newsgroup.
            >
            I asked this once on clc++m and had not got a convincing answer. [..]
            >
            As you may have noticed, I removed all mention of the C++ problem but
            left in the "compilatio n flags" request. That (and only that) is what
            I am suggesting asking about in the VC++ newsgroup.
            >
            Sorry about that! I wrongly quoted your response. My reply was to the
            original post. I wouldn't have said : "May be some
            guru will enlighten us! :)" to you! :)

            Comment

            • Victor Bazarov

              #7
              Re: const object declaration

              Abhishek Padmanabh wrote:
              [..] I wouldn't have said : "May be some
              guru will enlighten us! :)" to you! :)
              Hey, why not? I'll gladly take a guru's advice. After all it's not
              that often gurus grace us with their presence here...

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


              Comment

              • Bo Persson

                #8
                Re: const object declaration

                Abhishek Padmanabh wrote:
                :: On Dec 3, 7:54 pm, "Victor Bazarov" <v.Abaza...@com Acast.net>
                :: wrote:
                ::: benben wrote:
                ::::: [..]
                ::::: Also please let me know how to set the compilation flags in
                ::::: VC++ 2005 Express Edition IDE similar to the command line g++
                ::::: compilation flags as mentioned above.
                :::
                :::: That, im afraid, is better asked in the gcc group.
                :::
                ::: Or in the VC++ newsgroup.
                :::
                ::: V
                ::
                :: I asked this once on clc++m and had not got a convincing answer.
                :: It is mandated by the standard that a user-defined constructor
                :: must be provided. It is needed even though there are no members in
                :: the class. It just is as it is. I am not aware of the rationale
                :: behind this though.
                ::
                :: In case the class has a POD type, it would remain uninitialized
                :: (indeterminate value) when default constructor (by the compiler
                :: generated default constructor). In case of non-POD members, they
                :: would have been default initialized. An indeterminate POD type's
                :: value is rather useless but for non-POD types, it might make
                :: sense. I don't know about the rationale but may be it is based on
                :: something along these lines, to avoid complexity. I don't know for
                :: sure. May be some guru will enlighten us! :)

                Since no guru appeared, I will try to answer this. :-)

                The reason for PODs to be different, is that they behave like they do
                in C. Had C++ added initialization to types that C does not, it would
                have

                1) changed old behaviour of existing code
                and
                2) lost all benchmarks to C code.


                Bo Persson


                Comment

                • James Kanze

                  #9
                  Re: const object declaration

                  On Dec 3, 4:05 pm, Abhishek Padmanabh <abhishek.padma n...@gmail.com>
                  wrote:
                  I asked this once on clc++m and had not got a convincing
                  answer. It is mandated by the standard that a user-defined
                  constructor must be provided.
                  No it isn't. It is mandated that there be some non-trivial
                  initialization. In his case, something like:

                  X const obj = { 1 } ;

                  would be legal, for example.
                  It is needed even though there are no members in the class.
                  It just is as it is. I am not aware of the rationale behind
                  this though.
                  The requirement in the case of no data members is probably just
                  the result of an oversight. Since it's trivial to add a no-op
                  default constructor, however, it's no big problem.
                  In case the class has a POD type, it would remain
                  uninitialized (indeterminate value) when default constructor
                  (by the compiler generated default constructor).
                  In the case we're considering, the class is a POD.

                  --
                  James Kanze (GABI Software) email:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  • subramanian100in@yahoo.com, India

                    #10
                    Re: const object declaration

                    The following program DOES NOT COMPILE as mentioned earlier in the
                    beginning of the thread

                    #include <iostream>

                    using namespace std;

                    class X
                    {
                    public:
                    int val;
                    };

                    int main()
                    {
                    const X obj;

                    return 0;

                    }

                    However the following program COMPILES fine.

                    #include <iostream>

                    using namespace std;

                    class Test
                    {
                    public:
                    Test() { cout << "Test() default ctor" << endl; }
                    };

                    class X
                    {
                    public:
                    Test obj;
                    int val;
                    };

                    int main()
                    {
                    const X obj;

                    return 0;
                    }


                    I am unable to understand why this program compiles well and the
                    previous program does not compile.

                    Kindly explain.

                    Thanks
                    V.Subramanian

                    Comment

                    • James Kanze

                      #11
                      Re: const object declaration

                      On Dec 5, 7:15 am, "subramanian10. ..@yahoo.com, India"
                      <subramanian10. ..@yahoo.comwro te:
                      The following program DOES NOT COMPILE as mentioned earlier in the
                      beginning of the thread
                      #include <iostream>
                      using namespace std;
                      class X
                      {
                      public:
                      int val;
                      };
                      >
                      int main()
                      {
                      const X obj;
                      >
                      return 0;
                      }
                      However the following program COMPILES fine.
                      #include <iostream>
                      using namespace std;
                      class Test
                      {
                      public:
                      Test() { cout << "Test() default ctor" << endl; }
                      };
                      >
                      class X
                      {
                      public:
                      Test obj;
                      int val;
                      };
                      int main()
                      {
                      const X obj;
                      >
                      return 0;
                      }
                      I am unable to understand why this program compiles well and the
                      previous program does not compile.
                      I was about to say: because in the first example, the default
                      constructor is trivial (so an initialization is required),
                      whereas in the second it is not. On re-reading the standard,
                      however... in §8.5/9, it says "If no initializer is specified
                      for an object, and the object is of (possibly cv-qualified)
                      non-POD class type (or array thereof), the object shall be
                      default-initialized; if the object is of const-qualified type,
                      the underlying class type shall have a user-declared default
                      constructor." According to this, since X does not have a
                      user-declared default constructor, it shouldn't compile. But it
                      does, with all three of the compilers at my disposition (g++,
                      Sun CC and VC++).

                      I'll admit that this somewhat surprises me. I had always
                      thought (and apparently, the compilers agree with me) that the
                      rule was that the class must have a non-trivial default
                      constructor. Since the (compiler generated) default constructor
                      for X in the second example is non-trivial, the code would be
                      OK. Given that three compilers also disagree with what I've
                      just quoted in the standard, I wonder, too, if there isn't
                      something else that I've missed.

                      Thinking about it, I think what the rule should be is: an
                      initializer is required if the class has no user defined
                      default constructor, and has one or more sub-objects which have
                      trivial default constructors. That would allow const instances
                      without initializers for things like:

                      struct A
                      {
                      virtual void f() ;
                      } ;

                      or

                      struct B
                      {
                      std::vector< int v ;
                      } ;

                      . In both cases, all fields are sufficiently initialized (and
                      it was to allow such things that I thought the rule involved
                      trivial vs. non-trivial default constructor). And would cause
                      your second example to fail to compile, since X::val has a
                      trivial default constructor. (It doesn't cover all cases, of
                      course. For example:

                      struct A
                      {
                      virtual void f() ; // means default ctor non-trivial.
                      int i ;
                      } ;

                      struct B
                      {
                      A a ;
                      } ;

                      B const b ;

                      would still create a const object in which A::i wasn't
                      initialized.)

                      --
                      James Kanze (GABI Software) email:james.kan ze@gmail.com
                      Conseils en informatique orientée objet/
                      Beratung in objektorientier ter Datenverarbeitu ng
                      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                      Comment

                      Working...