Legal or not?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christoph Rabel

    Legal or not?

    Hi!

    I tried to compile the following code with Comeau, but it gave me a
    puzzling error(The code makes no sense, it is just to show what puzzles me)

    class Base
    {
    public:
    Base();
    Base(int);
    };

    class Derived : public Base
    {
    public:
    Derived(int i)
    {
    Base(0); // Compiles fine, but

    Base::Base(0); // Does not compile, Comeau says:

    // error: a constructor or destructor may not have its
    // address taken

    }
    };

    Is the line valid or not?

    greets,

    Christoph

  • Jakob Bieling

    #2
    Re: Legal or not?

    "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message
    news:3f5753d2$0 $29344$3b214f66 @aconews.univie .ac.at...[color=blue]
    > Hi!
    >
    > I tried to compile the following code with Comeau, but it gave me a
    > puzzling error(The code makes no sense, it is just to show what puzzles[/color]
    me)[color=blue]
    >
    > class Base
    > {
    > public:
    > Base();
    > Base(int);
    > };
    >
    > class Derived : public Base
    > {
    > public:
    > Derived(int i)
    > {
    > Base(0); // Compiles fine, but[/color]

    Creates a temporary object of type 'Base'.
    [color=blue]
    > Base::Base(0); // Does not compile, Comeau says:[/color]

    I am not sure why it said you should not take the address, because I
    would have thought the line above would mean a call to the c'tor, which
    cannot work.
    [color=blue]
    > // error: a constructor or destructor may not have its
    > // address taken
    >
    > }
    > };
    >
    > Is the line valid or not?[/color]

    Even though I do not quite understand why Comeau prints /that/ error
    message, I still think it is invalid.

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)


    Comment

    • Ron Natalie

      #3
      Re: Legal or not?


      "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message news:3f5753d2$0 $29344$3b214f66 @aconews.univie .ac.at...
      [color=blue]
      > Base(0); // Compiles fine, but[/color]

      This probably does not do what you think. This creates a temporary object of
      type Base (initialized with a 0). It is not, as you might be expecting, a call
      to the Base class constructor on the curent object.
      [color=blue]
      > Base::Base(0); // Does not compile, Comeau says:
      >[/color]
      This line isn't valid. Base::Base is not valid at all in this context.
      Comeau's error message is a bit obtuse but since it's not a valid
      construct it's just trying to take a stab at what it thinks you're
      trying to do.

      What you really want is:

      Derived(int i) : Base(0) { }


      Comment

      • Ron Natalie

        #4
        Re: Legal or not?


        "Jakob Bieling" <netsurf@gmy.ne t> wrote in message news:bj7kde$b56 $04$1@news.t-online.com...[color=blue][color=green][color=darkred]
        >> > Base::Base(0); // Does not compile, Comeau says:[/color][/color]
        >
        > I am not sure why it said you should not take the address, because I
        > would have thought the line above would mean a call to the c'tor, which
        > cannot work.[/color]

        Can't mean that, because constructors don't have names. Base::Base
        just isn't a valid sequence here.


        Comment

        • Christoph Rabel

          #5
          Re: Legal or not?

          Ron Natalie wrote:[color=blue]
          > "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message news:3f5753d2$0 $29344$3b214f66 @aconews.univie .ac.at...[color=green]
          >> Base(0); // Compiles fine, but[/color]
          >
          > This probably does not do what you think. This creates a temporary object of
          > type Base (initialized with a 0). It is not, as you might be expecting, a call
          > to the Base class constructor on the curent object.[/color]

          Oh, I know that. I simply was confused that I am allowed to write
          Base(0); but not to write Base::Base(0);
          [color=blue][color=green]
          >> Base::Base(0); // Does not compile, Comeau says:
          >>[/color]
          > This line isn't valid. Base::Base is not valid at all in this context.
          > Comeau's error message is a bit obtuse but since it's not a valid
          > construct it's just trying to take a stab at what it thinks you're
          > trying to do.[/color]

          I'd like to understand why the first line is valid and the second not. I
          thought, that they are essentially "equal".

          Is it because of 12.1.2, Constructors have no name and are never found
          during name lookup?

          thx,

          Christoph

          Comment

          • Jakob Bieling

            #6
            Re: Legal or not?

            "Ron Natalie" <ron@sensor.com > wrote in message
            news:3f5755e9$0 $23273$9a6e19ea @news.newshosti ng.com...[color=blue]
            >
            > "Jakob Bieling" <netsurf@gmy.ne t> wrote in message[/color]
            news:bj7kde$b56 $04$1@news.t-online.com...[color=blue][color=green][color=darkred]
            > >> > Base::Base(0); // Does not compile, Comeau says:[/color]
            > >
            > > I am not sure why it said you should not take the address, because I
            > > would have thought the line above would mean a call to the c'tor, which
            > > cannot work.[/color]
            >
            > Can't mean that, because constructors don't have names. Base::Base
            > just isn't a valid sequence here.[/color]


            I know, but most people who write that think they can call c'tors
            explicitly, which is also what it looks like: a call to that c'tor
            (regardsless of the fact that this is illegal). So I would have expected the
            compiler to print out something along that line.

            regards
            --
            jb

            (replace y with x if you want to reply by e-mail)


            Comment

            • Jakob Bieling

              #7
              Re: Legal or not?

              "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message
              news:3f575885$0 $25930$3b214f66 @aconews.univie .ac.at...[color=blue]
              > Ron Natalie wrote:[color=green]
              > > "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message[/color][/color]
              news:3f5753d2$0 $29344$3b214f66 @aconews.univie .ac.at...[color=blue][color=green][color=darkred]
              > >> Base(0); // Compiles fine, but[/color]
              > >
              > > This probably does not do what you think. This creates a temporary[/color][/color]
              object of[color=blue][color=green]
              > > type Base (initialized with a 0). It is not, as you might be expecting,[/color][/color]
              a call[color=blue][color=green]
              > > to the Base class constructor on the curent object.[/color]
              >
              > Oh, I know that. I simply was confused that I am allowed to write
              > Base(0); but not to write Base::Base(0);
              >[color=green][color=darkred]
              > >> Base::Base(0); // Does not compile, Comeau says:
              > >>[/color]
              > > This line isn't valid. Base::Base is not valid at all in this context.
              > > Comeau's error message is a bit obtuse but since it's not a valid
              > > construct it's just trying to take a stab at what it thinks you're
              > > trying to do.[/color]
              >
              > I'd like to understand why the first line is valid and the second not. I
              > thought, that they are essentially "equal".[/color]


              Suppose you had a typedef 'test' for 'int' inside 'Base'. Then you could
              do this: Base::test (0). Now you did Base::Base (0), so you are trying to
              find something inside the 'Base' namespace. But there is nothing you could
              find like that; the 'Base' class is not inside the 'Base' namespace. This is
              why the two lines are not equal at all.

              hth
              --
              jb

              (replace y with x if you want to reply by e-mail)


              Comment

              • Ron Natalie

                #8
                Re: Legal or not?


                "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message news:3f575885$0 $25930[color=blue]
                > Oh, I know that. I simply was confused that I am allowed to write
                > Base(0); but not to write Base::Base(0);[/color]

                Base is a type name in this context, it's legal. Base::Base is not.
                [color=blue]
                > I'd like to understand why the first line is valid and the second not. I
                > thought, that they are essentially "equal".
                > Is it because of 12.1.2, Constructors have no name and are never found
                > during name lookup?[/color]

                Paritally. The constructor doesn't have a name. Base::Base doesn't
                refer to it, Base doesn't refer to it. Base works becuase it is a type
                name. Base::Base is nonsense, there is no legitimate qualified identifier
                of that name.


                Comment

                • jeffc

                  #9
                  Re: Legal or not?


                  "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message
                  news:3f5753d2$0 $29344$3b214f66 @aconews.univie .ac.at...[color=blue]
                  > Hi!
                  >
                  > I tried to compile the following code with Comeau, but it gave me a
                  > puzzling error(The code makes no sense, it is just to show what puzzles[/color]
                  me)[color=blue]
                  >
                  > class Base
                  > {
                  > public:
                  > Base();
                  > Base(int);
                  > };
                  >
                  > class Derived : public Base
                  > {
                  > public:
                  > Derived(int i)
                  > {
                  > Base(0); // Compiles fine, but
                  >
                  > Base::Base(0); // Does not compile, Comeau says:
                  >
                  > // error: a constructor or destructor may not have its
                  > // address taken
                  >
                  > }
                  > };
                  >
                  > Is the line valid or not?[/color]

                  I don't understand the error message, but it's just the compiler struggling
                  to make sense out of what you really wanted to do (as I am :-) I'm sure
                  Base(0); isn't what you want to do either. You need to read up on
                  "initialize r list". You need to "call" the base constructor in a special -
                  NOT the way you attempted.


                  Comment

                  • jeffc

                    #10
                    Re: Legal or not?


                    "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message
                    news:3f575885$0 $25930$3b214f66 @aconews.univie .ac.at...[color=blue]
                    > Ron Natalie wrote:[color=green]
                    > > "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message[/color][/color]
                    news:3f5753d2$0 $29344$3b214f66 @aconews.univie .ac.at...[color=blue][color=green][color=darkred]
                    > >> Base(0); // Compiles fine, but[/color]
                    > >
                    > > This probably does not do what you think. This creates a temporary[/color][/color]
                    object of[color=blue][color=green]
                    > > type Base (initialized with a 0). It is not, as you might be expecting,[/color][/color]
                    a call[color=blue][color=green]
                    > > to the Base class constructor on the curent object.[/color]
                    >
                    > Oh, I know that. I simply was confused that I am allowed to write
                    > Base(0); but not to write Base::Base(0);[/color]

                    Because there is no "Base" defined inside the "Base".
                    [color=blue]
                    > I'd like to understand why the first line is valid and the second not. I
                    > thought, that they are essentially "equal".
                    >
                    > Is it because of 12.1.2, Constructors have no name and are never found
                    > during name lookup?[/color]

                    No, it's because Base is in the global scope, and nowhere else. Under what
                    conditions would you normally use the :: operator?


                    Comment

                    • jeffc

                      #11
                      Re: Legal or not?


                      "Jakob Bieling" <netsurf@gmy.ne t> wrote in message
                      news:bj7lbf$t4d $01$1@news.t-online.com...[color=blue]
                      > I know, but most people who write that think they can call c'tors
                      > explicitly, which is also what it looks like: a call to that c'tor
                      > (regardsless of the fact that this is illegal). So I would have expected[/color]
                      the[color=blue]
                      > compiler to print out something along that line.[/color]

                      I prefer to think of it that you can call a constructor explicitly in the
                      initializer list. From the user's point of view (if not the compiler's),
                      it's indistinguishab le from any other function call (someone correct me if
                      I'm wrong.)


                      Comment

                      • Ron Natalie

                        #12
                        Re: Legal or not?


                        "jeffc" <nobody@nowhere .com> wrote in message news:3f57991a_4 @news1.prserv.n et...[color=blue]
                        >
                        > "Jakob Bieling" <netsurf@gmy.ne t> wrote in message
                        > news:bj7lbf$t4d $01$1@news.t-online.com...[color=green]
                        > > I know, but most people who write that think they can call c'tors
                        > > explicitly, which is also what it looks like: a call to that c'tor
                        > > (regardsless of the fact that this is illegal). So I would have expected[/color]
                        > the[color=green]
                        > > compiler to print out something along that line.[/color]
                        >
                        > I prefer to think of it that you can call a constructor explicitly in the
                        > initializer list.[/color]

                        You may think of it that way, but it is wrong. It is NOT calling the constructor
                        there and thinking that way just adds to the confusion. All you are doing is providing
                        initializers for the objects. Specifically, if you think that you are calling there, you might
                        think that the order that you specify the initializers have some meaning. Further you
                        can specify initializers in the list for things that don't even have constructors.
                        [color=blue]
                        > From the user's point of view (if not the compiler's),
                        > it's indistinguishab le from any other function call (someone correct me if
                        > I'm wrong.)[/color]

                        I contend you are wrong. It's nothing like a function call. It's more like remembering
                        what the initializers are so when it's time to invoke the constructor we know what they
                        are.


                        Comment

                        • Christoph Rabel

                          #13
                          Re: Legal or not?

                          Jakob Bieling wrote:[color=blue]
                          > "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message
                          > news:3f575885$0 $25930$3b214f66 @aconews.univie .ac.at...
                          >
                          > Suppose you had a typedef 'test' for 'int' inside 'Base'. Then you could
                          > do this: Base::test (0). Now you did Base::Base (0), so you are trying to
                          > find something inside the 'Base' namespace. But there is nothing you could
                          > find like that; the 'Base' class is not inside the 'Base' namespace. This is
                          > why the two lines are not equal at all.[/color]

                          Doesnt this contradict 9.2:

                          "A classname is inserted into the scope in which it is
                          declared immediately after the classname is seen.
                          The classname is also inserted into the scope of the class
                          itself."

                          Or am I misunderstandin g something here?

                          Christoph

                          Comment

                          • jeffc

                            #14
                            Re: Legal or not?


                            "Ron Natalie" <ron@sensor.com > wrote in message
                            news:3f57a12a$0 $23178$9a6e19ea @news.newshosti ng.com...[color=blue][color=green]
                            > >
                            > > I prefer to think of it that you can call a constructor explicitly in[/color][/color]
                            the[color=blue][color=green]
                            > > initializer list.[/color]
                            >
                            > You may think of it that way, but it is wrong. It is NOT calling the[/color]
                            constructor[color=blue]
                            > there and thinking that way just adds to the confusion.[/color]

                            On the contrary, it makes initializer lists more clear to most people.
                            [color=blue]
                            > Specifically, if you think that you are calling there, you might
                            > think that the order that you specify the initializers have some meaning.[/color]

                            One might, but I wouldn't. There aren't any semicolons there, so I wouldn't
                            think that. That's beside my point.
                            [color=blue]
                            > Further you can specify initializers in the list for things that don't[/color]
                            even have constructors.

                            Again, irrelevant and beside my point. Those things are a syntactical
                            convenience (just like int i(1);) and couldn't possibly be function calls.
                            [color=blue][color=green]
                            > > From the user's point of view (if not the compiler's),
                            > > it's indistinguishab le from any other function call (someone correct me[/color][/color]
                            if[color=blue][color=green]
                            > > I'm wrong.)[/color]
                            >
                            > I contend you are wrong. It's nothing like a function call. It's more[/color]
                            like remembering[color=blue]
                            > what the initializers are so when it's time to invoke the constructor we[/color]
                            know what they[color=blue]
                            > are.[/color]

                            "It's nothing like a function call." You can't back that one up. It's very
                            much like a function call. Run it through your debugger sometime and
                            explain to me why it doesn't act like a function call from the user's point
                            of view.


                            Comment

                            • jeffc

                              #15
                              Re: Legal or not?


                              "Ron Natalie" <ron@sensor.com > wrote in message
                              news:3f57a12a$0 $23178$9a6e19ea @news.newshosti ng.com...[color=blue][color=green][color=darkred]
                              >> >[/color]
                              > > I prefer to think of it that you can call a constructor explicitly in[/color][/color]
                              the[color=blue][color=green]
                              > > initializer list.[/color]
                              >
                              > You may think of it that way, but it is wrong. It is NOT calling the[/color]
                              constructor[color=blue]
                              > there and thinking that way just adds to the confusion. All you are[/color]
                              doing is providing[color=blue]
                              > initializers for the objects. Specifically, if you think that you are[/color]
                              calling there, you might[color=blue]
                              > think that the order that you specify the initializers have some meaning.[/color]

                              As further evidence that this is really a red herring, consider the
                              following:
                              int a(){return 1;}
                              int b(){return 2;}
                              int c(){return 3;}

                              int x = a() + b() * c();
                              compare with
                              int x = c() * b() + a();
                              compare with
                              int x = a() * b() + c();

                              According to your logic, you should not think of these as function calls
                              because then you might think that the order you specify the names has some
                              meaning. In both this case and the initializer list case, functions are
                              being called (constructors *are* special functions), and the order in which
                              they're called depends on something other than the sequential order in which
                              they appear on the page.


                              Comment

                              Working...