I got strange resule while compiling this code...

Discussion in 'C++' started by =?gb2312?B?tdjH8reitq+7+g==?=, Apr 7, 2007.

  1. Who can tell me the result in your compiler? Is it a bug or standard
    beheiver?
    class A
    {
    template<int>
    void a();

    };

    class B:A
    {

    };

    template<>
    void B::a<0>()
    {
    B *b=this; // comment this line!

    }

    And what would happen when you comment the marked line?

    (I got some stupid result in my compiler... Who can answer that is not
    a bug of the compiler? I will show you the stupid answer after some
    replies. )
    =?gb2312?B?tdjH8reitq+7+g==?=, Apr 7, 2007
    #1
    1. Advertising

  2. =?gb2312?B?tdjH8reitq+7+g==?=

    Rolf Magnus Guest

    地çƒå‘动机 wrote:

    > Who can tell me the result in your compiler?


    Well, in my compiler, the result is:

    error: template-id 'a<0>' for 'void B::a()' does not match any template
    declaration
    error: invalid function declaration

    which is pretty much what I would have expected.


    > Is it a bug or standard
    > beheiver?
    > class A
    > {
    > template<int>
    > void a();
    >
    > };
    >
    > class B:A
    > {
    >
    > };
    >
    > template<>
    > void B::a<0>()
    > {
    > B *b=this; // comment this line!
    >
    > }
    >
    > And what would happen when you comment the marked line?


    Exactly the same.

    > (I got some stupid result in my compiler... Who can answer that is not
    > a bug of the compiler?


    I don't know if "some stupid result" is a bug, since you didn't tell what
    you mean by that.

    > I will show you the stupid answer after some replies. )
    Rolf Magnus, Apr 7, 2007
    #2
    1. Advertising

  3. =?gb2312?B?tdjH8reitq+7+g==?=

    Salt_Peter Guest

    On Apr 7, 12:03 pm, "地çƒå‘动机" <> wrote:
    > Who can tell me the result in your compiler? Is it a bug or standard
    > beheiver?
    > class A
    > {
    > template<int>
    > void a();


    the templated function is private.

    >
    > };
    >
    > class B:A
    > {
    >
    > };


    private inheritence

    >
    > template<>
    > void B::a<0>()
    > {
    > B *b=this; // comment this line!
    >
    > }


    B::a< int >() was never declared, only A::a< int >() was

    >
    > And what would happen when you comment the marked line?


    what for - the above won't compile

    >
    > (I got some stupid result in my compiler... Who can answer that is not
    > a bug of the compiler? I will show you the stupid answer after some
    > replies. )


    answer?
    Since you haven't asked any question that makes sense yet, take the
    following and try asking again...

    #include<iostream>

    class A
    {
    public:
    template< const int N >
    void a() const;
    };

    template< const int N >
    void A::a() const
    {
    std::cout << "N = " << N << std::endl;
    std::cout << this << std::endl;
    }

    class B : public A
    {
    };

    int main() {
    B b;
    b.a< 0 >();
    }
    Salt_Peter, Apr 7, 2007
    #3
  4. On 4ÔÂ8ÈÕ, ÉÏÎç12ʱ54·Ö, Rolf Magnus <> wrote:
    > µØÇò·¢¶¯»ú wrote:
    > > Who can tell me the result in your compiler?

    >
    > Well, in my compiler, the result is:
    >
    > error: template-id 'a<0>' for 'void B::a()' does not match any template
    > declaration
    > error: invalid function declaration
    >
    > which is pretty much what I would have expected.
    >
    >
    >
    >
    >
    > > Is it a bug or standard
    > > beheiver?
    > > class A
    > > {
    > > template<int>
    > > void a();

    >
    > > };

    >
    > > class B:A
    > > {

    >
    > > };

    >
    > > template<>
    > > void B::a<0>()
    > > {
    > > B *b=this; // comment this line!

    >
    > > }

    >
    > > And what would happen when you comment the marked line?

    >
    > Exactly the same.
    >
    > > (Igotsome stupid result in my compiler... Who can answer that is not
    > > a bug of the compiler?

    >
    > I don't know if "some stupid result" is a bug, since you didn't tell what
    > you mean by that.
    >
    >
    >
    > > I will show you the stupid answer after some replies. )- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -


    Good, now I am sure that I got from my compiler - Visual C++ 2005 - is
    a bug, because the result is:

    error C2440: 'initializing' : cannot convert from 'A *const ' to 'B *'
    Cast from base to derived requires dynamic_cast or static_cast

    and if I comment the line inside B::a<0>(), everything ok.

    It seems that the compiler consider that the B::a<0>() to be A::a<0>().
    =?gb2312?B?tdjH8reitq+7+g==?=, Apr 8, 2007
    #4
  5. On 4ÔÂ8ÈÕ, ÉÏÎç3ʱ05·Ö, "Salt_Peter" <pj_h...@yahoo..com> wrote:
    > On Apr 7, 12:03 pm, "µØÇò·¢¶¯»ú" <> wrote:
    >
    > > Who can tell me the result in your compiler? Is it a bug or standard
    > > beheiver?
    > > class A
    > > {
    > > template<int>
    > > void a();

    >
    > the templated function is private.
    >
    >
    >
    > > };

    >
    > > class B:A
    > > {

    >
    > > };

    >
    > private inheritence
    >
    >
    >
    > > template<>
    > > void B::a<0>()
    > > {
    > > B *b=this; // comment this line!

    >
    > > }

    >
    > B::a< int >() was never declared, only A::a< int >() was
    >
    >
    >
    > > And what would happen when you comment the marked line?

    >
    > what for - the above won't compile
    >
    >
    >
    > > (I got some stupid result in my compiler... Who can answer that is not
    > > a bug of the compiler? I will show you the stupid answer after some
    > > replies. )

    >
    > answer?
    > Since you haven't asked any question that makes sense yet, take the
    > following and try asking again...
    >
    > #include<iostream>
    >
    > class A
    > {
    > public:
    > template< const int N >
    > void a() const;
    >
    > };
    >
    > template< const int N >
    > void A::a() const
    > {
    > std::cout << "N = " << N << std::endl;
    > std::cout << this << std::endl;
    >
    > }
    >
    > class B : public A
    > {
    >
    > };
    >
    > int main() {
    > B b;
    > b.a< 0 >();
    >
    >
    >
    > }- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -


    Your modify is correct and perfect. But I just talking about a stupid
    bug of my compiler(VC2005). And now I believe it is not my
    misunderstand of the language.

    My compiler consider the error of the code is that I cast the 'this'
    pointer to B * from A *(I don't know why it consider the this pointer
    inside B::a() is a B *). And if I comments the line of casting, it can
    be compiled without event a warning!
    =?gb2312?B?tdjH8reitq+7+g==?=, Apr 8, 2007
    #5
  6. =?gb2312?B?tdjH8reitq+7+g==?=

    Salt_Peter Guest

    On Apr 7, 9:34 pm, "地çƒå‘动机" <> wrote:
    > On 4月8æ—¥, 上åˆ3æ—¶05分, "Salt_Peter" <> wrote:
    >
    >
    >
    > > On Apr 7, 12:03 pm, "地çƒå‘动机" <> wrote:

    >
    > > > Who can tell me the result in your compiler? Is it a bug or standard
    > > > beheiver?
    > > > class A
    > > > {
    > > > template<int>
    > > > void a();

    >
    > > the templated function is private.

    >
    > > > };

    >
    > > > class B:A
    > > > {

    >
    > > > };

    >
    > > private inheritence

    >
    > > > template<>
    > > > void B::a<0>()
    > > > {
    > > > B *b=this; // comment this line!

    >
    > > > }

    >
    > > B::a< int >() was never declared, only A::a< int >() was

    >
    > > > And what would happen when you comment the marked line?

    >
    > > what for - the above won't compile

    >
    > > > (I got some stupid result in my compiler... Who can answer that is not
    > > > a bug of the compiler? I will show you the stupid answer after some
    > > > replies. )

    >
    > > answer?
    > > Since you haven't asked any question that makes sense yet, take the
    > > following and try asking again...

    >
    > > #include<iostream>

    >
    > > class A
    > > {
    > > public:
    > > template< const int N >
    > > void a() const;

    >
    > > };

    >
    > > template< const int N >
    > > void A::a() const
    > > {
    > > std::cout << "N = " << N << std::endl;
    > > std::cout << this << std::endl;

    >
    > > }

    >
    > > class B : public A
    > > {

    >
    > > };

    >
    > > int main() {
    > > B b;
    > > b.a< 0 >();

    >
    > > }- éšè—被引用文字 -

    >
    > > - 显示引用的文字 -

    >
    > Your modify is correct and perfect. But I just talking about a stupid
    > bug of my compiler(VC2005). And now I believe it is not my
    > misunderstand of the language.
    >
    > My compiler consider the error of the code is that I cast the 'this'
    > pointer to B * from A *(I don't know why it consider the this pointer
    > inside B::a() is a B *). And if I comments the line of casting, it can
    > be compiled without event a warning!


    Your code compiled without a warning? i doubt that emphatically.
    Let me put it to you this way: in your code: A::a() was declared but
    not defined
    and B::a() does not exist. It was never declared in class B.

    Obviously if you declared and then defined both A::a() and B::a()
    properly,
    the this pointer would have to be a B* in B::a().
    I don't see why this fact puzzles you?
    Salt_Peter, Apr 8, 2007
    #6
  7. On 4ÔÂ8ÈÕ, ÏÂÎç1ʱ58·Ö, "Salt_Peter" <pj_h...@yahoo..com> wrote:
    > On Apr 7, 9:34 pm, "µØÇò·¢¶¯»ú" <> wrote:
    >
    >
    >
    > > On 4ÔÂ8ÈÕ, ÉÏÎç3ʱ05·Ö, "Salt_Peter" <> wrote:

    >
    > > > On Apr 7, 12:03 pm, "µØÇò·¢¶¯»ú" <> wrote:

    >
    > > > > Who can tell me the result in your compiler? Is it a bug or standard
    > > > > beheiver?
    > > > > class A
    > > > > {
    > > > > template<int>
    > > > > void a();

    >
    > > > the templated function is private.

    >
    > > > > };

    >
    > > > > class B:A
    > > > > {

    >
    > > > > };

    >
    > > > private inheritence

    >
    > > > > template<>
    > > > > void B::a<0>()
    > > > > {
    > > > > B *b=this; // comment this line!

    >
    > > > > }

    >
    > > > B::a< int >() was never declared, only A::a< int >() was

    >
    > > > > And what would happen when you comment the marked line?

    >
    > > > what for - the above won't compile

    >
    > > > > (I got some stupid result in my compiler... Who can answer that is not
    > > > > a bug of the compiler? I will show you the stupid answer after some
    > > > > replies. )

    >
    > > > answer?
    > > > Since you haven't asked any question that makes sense yet, take the
    > > > following and try asking again...

    >
    > > > #include<iostream>

    >
    > > > class A
    > > > {
    > > > public:
    > > > template< const int N >
    > > > void a() const;

    >
    > > > };

    >
    > > > template< const int N >
    > > > void A::a() const
    > > > {
    > > > std::cout << "N = " << N << std::endl;
    > > > std::cout << this << std::endl;

    >
    > > > }

    >
    > > > class B : public A
    > > > {

    >
    > > > };

    >
    > > > int main() {
    > > > B b;
    > > > b.a< 0 >();

    >
    > > > }- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

    >
    > > > - ÏÔʾÒýÓõÄÎÄ×Ö -

    >
    > > Your modify is correct and perfect. But I just talking about a stupid
    > > bug of my compiler(VC2005). And now I believe it is not my
    > > misunderstand of the language.

    >
    > > My compiler consider the error of the code is that I cast the 'this'
    > > pointer to B * from A *(I don't know why it consider the this pointer
    > > inside B::a() is a B *). And if I comments the line of casting, it can
    > > be compiled without event a warning!

    >
    > Your code compiled without a warning? i doubt that emphatically.
    > Let me put it to you this way: in your code: A::a() was declared but
    > not defined
    > and B::a() does not exist. It was never declared in class B.
    >
    > Obviously if you declared and then defined both A::a() and B::a()
    > properly,
    > the this pointer would have to be a B* in B::a().
    > I don't see why this fact puzzles you?


    I am sorry I have make a mistake. "I don't know why it consider the
    this
    pointer inside B::a() is a B *" should be "I don't know why it
    consider
    the this pointer inside B::a() is an A *". And it it is that I puzzles
    for.
    =?gb2312?B?tdjH8reitq+7+g==?=, Apr 8, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ^CrazyCoder^
    Replies:
    3
    Views:
    7,257
    Terrence Benade
    Sep 15, 2003
  2. Arnab Nandi
    Replies:
    5
    Views:
    2,878
  3. Sam
    Replies:
    0
    Views:
    414
  4. Victor Lamberty

    I got it to compile but it did somthing strange

    Victor Lamberty, Jul 22, 2003, in forum: C Programming
    Replies:
    8
    Views:
    390
    Herbert Rosenau
    Jul 31, 2003
  5. Yasin Cepeci
    Replies:
    2
    Views:
    430
    Yasin cepeci
    Jun 7, 2007
Loading...

Share This Page