473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

scope question

Hi,

just being curious:
Anyone having a clue why the commented lines do not compile?

Thanks,

Christof

class B {
public:
int x;
};
template<int tclass TB: public B {
public:
int y;
TB() {
// Does compile
x = 0;
B::x = 0;
}
};
template<int tclass D: public TB<t{
public:
D() {
// Does not compile:
//x = 0;
//B::x = 0;
//y = 0;
// Does compile
this->B::x = 0;
this->x = 0;
D<t>::x = 0;
TB<t>::x = 0;
this->y = 0;
TB<t>::y = 0;
}
};
int main() {}
Nov 28 '07 #1
5 1258
On 2007-11-28 02:54:12 -0500, Christof Warlich
<cw******@alcatel-lucent.desaid:
Hi,

just being curious:
Anyone having a clue why the commented lines do not compile?

Thanks,

Christof

class B {
public:
int x;
};
template<int tclass TB: public B {
public:
int y;
TB() {
// Does compile
x = 0;
B::x = 0;
}
};
template<int tclass D: public TB<t{
public:
D() {
// Does not compile:
//x = 0;
//B::x = 0;
//y = 0;
// Does compile
this->B::x = 0;
this->x = 0;
D<t>::x = 0;
TB<t>::x = 0;
this->y = 0;
TB<t>::y = 0;
}
};
int main() {}
Google "two phase name lookup C++"

--

-kira

Nov 28 '07 #2
On Nov 28, 10:54, Christof Warlich <cwarl...@alcatel-lucent.dewrote:
Hi,

just being curious:
Anyone having a clue why the commented lines do not compile?

Thanks,

Christof

class B {
public:
int x;};

template<int tclass TB: public B {
public:
int y;
TB() {
// Does compile
x = 0;
B::x = 0;
}};

template<int tclass D: public TB<t{
public:
D() {
// Does not compile:
//x = 0;
//B::x = 0;
//y = 0;
// Does compile
this->B::x = 0;
this->x = 0;
D<t>::x = 0;
TB<t>::x = 0;
this->y = 0;
TB<t>::y = 0;
}};

int main() {}
Try changing line
this->B::x = 0;
to
this->B::x = "The conversion is impossible!";

Still compiles. :-) Try even more: change
this->B::x = 0;
to
this->std::iostream::x = "Still compiles...";

And if you replace :public TB<twith :public TB<2it starts
compiling commented strings...

templates are soo tricky :-) If you play with them a bit, you can
deduce some rules, but for strict ones you should consult standard, i
think.
After some experiments i deduced that compiler delays checking of this-
>something correctness to instantation of template, as well as
instantation of templated parents. So he merely doesnt see that B is
our parent and x and y are member variables when compiling D's
constructor.
Nov 28 '07 #3
Pavel Shved schrieb:
Try changing line
> this->B::x = 0;
to
this->B::x = "The conversion is impossible!";

Still compiles. :-) Try even more: change
> this->B::x = 0;
to
this->std::iostream::x = "Still compiles...";

And if you replace :public TB<twith :public TB<2it starts
compiling commented strings...
oh, oh - I shouldn't have asked: For decades, I felt so
comfortable with my naive belief in C++ ;-(.

Anyway, thanks a lot for this insight, though I'd better
try to forget .... ;-).
Nov 28 '07 #4
On Nov 28, 12:16, Christof Warlich <cwarl...@alcatel-lucent.dewrote:
Pavel Shved schrieb:
Try changing line
this->B::x = 0;
to
this->B::x = "The conversion is impossible!";
Still compiles. :-) Try even more: change
this->B::x = 0;
to
this->std::iostream::x = "Still compiles...";
And if you replace :public TB<twith :public TB<2it starts
compiling commented strings...

oh, oh - I shouldn't have asked: For decades, I felt so
comfortable with my naive belief in C++ ;-(.
Oh, no, you don't need to get disappointed with C++. The reason why
is described in C++-faq-lite, which link to you may easilly find in
google; check Templates section. I'd post it here, but i consider
unfair repeating what i've learned from experience of this group
subscribers.
Nov 28 '07 #5
On Nov 28, 8:54 am, Christof Warlich <cwarl...@alcatel-lucent.de>
wrote:
just being curious:
Anyone having a clue why the commented lines do not compile?
The difference is due to dependent vs. non-dependent name
look-up. Basically, the compiler separates names in a template
into two categories, dependent and non-dependent. If the name
is non-dependent, it is lookup up at the point where the
template is defined. If it is dependent, it is looked up when
the template is instantiated, using ADN, and ony ADN.

Basically, a name is dependent if it is used in a context which
depends on the template arguments. The exact rules are a bit
complicated, but that's the gist of it.
class B {
public:
int x;};
template<int tclass TB: public B {
Note that B is not dependent. Regardless of the instantiation
argument, B is B, and cannot change. So the compiler will
consider it during non-dependent name look-up.
public:
int y;
TB() {
// Does compile
x = 0;
B::x = 0;
Obviously:-). The compiler finds the x in B, in both cases.
}};
template<int tclass D: public TB<t{
Here, on the other hand, the base class is dependent. The
compiler cannot take it into account for non-dependent name
lookup, since it has no idea what could be in it.
public:
D() {
// Does not compile:
//x = 0;
//B::x = 0;
//y = 0;
That's because the names above are non-dependent, and are looked
up immediately, here, at the point of definition. And because,
at this time, the compiler cannot consider the base class,
because it doesn't know what will or will not be in it. (There
could be an explicit specialization that it hasn't seen yet, for
example.)

In the first and third cases, it's a simple case of the compiler
not finding the name at all. In the second, the B:: tells the
compiler to look in B, but the compiler doesn't have (or doesn't
know that it has) a base of type B, so it doesn't have an object
to associate with the x it finds in B.
// Does compile
this->B::x = 0;
this->x = 0;
D<t>::x = 0;
TB<t>::x = 0;
this->y = 0;
TB<t>::y = 0;
All of these introduce a dependent context, so name look-up is
defered until instantiation.
}};
int main() {}
Try adding some instantiations:

template<class TB<0{} ;

int main()
{
TD< 0 doh ;
}

That should cause a lot of errors. And also show why the
compiler can't look at the base class in non-dependent name
lookup.

Functions are even more fun, since whether a function is
dependent or not depends on its arguments. So we get things
like:

class A {} ;
void f( int ) ;
void f( A ) ;

template< typename T >
class TBase
{
public:
void f( int ) ;
void f( T ) ;
} ;

template< typename T >
class TDerived : public TBase
{
public:
void g()
{
this->f( 43 ) ; // calls TBase<>::f( int )
// (if there is one)
f( 43 ) ; // calls ::f(int)
this->f( A() ) ; // calls TBase<>::f( A )
// (if there is one)
f( A() ) ; // calls ::f(A), even if
// instantiated for A.
f( T() ) ; // calls TBase<>::f( T ), even
// if instantiated for A.
}
} ;

Note that if you instantiate TDerived<A>, then the last two
function calls in TDerived<>::g() are exactly the same, same
name, same arguments. Except that one calls the global function
(because it doesn't depend on T), and the other calls the
function in the base class (because it does depend on T, so it's
name lookup includes the base class).

Now throw in a few overloads and conversions, and let operator
overload resolution work with different sets of functions, and
create some real confusion. (If any of a function's arguments
are dependent, the function name is looked up in a dependent
fashion.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 28 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
6
by: Razvan | last post by:
Hi, I took a C++ test and I found the following question: Q. Inside a class member function definition, which scope is searched
9
by: Vipul Jain | last post by:
Can any one please tell me what is the difference between global scope of an variable and file scope of an variable. Vipul
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
165
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But...
6
by: Frank Silvermann | last post by:
I have taken an extraordinary leap into the modern world by purchasing webspace. In addition to my private concerns, I would like to make a part to which others, e.g. my nieces and ex-wife, can...
7
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
2
by: Michael | last post by:
Hello, I have a newbie question about class scope. I am writting a little program that will move files to one of two empty folders. I am having a hard time understanding scope. So this will be a...
20
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.