I got strange resule while compiling this code...

  • Thread starter =?gb2312?B?tdjH8reitq+7+g==?=
  • Start date
?

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

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. )
 
R

Rolf Magnus

地çƒå‘动机 said:
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.
 
S

Salt_Peter

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 said:
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 >();
}
 
?

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

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.






Exactly the same.


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




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

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

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==?=

the templated function is private.






private inheritence





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




what for - the above won't compile




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!
 
S

Salt_Peter

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?
 
?

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

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top