473,394 Members | 1,699 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,394 software developers and data experts.

non-dependent vs. dependent template names

See it a lot but haven't learn the difference between this two in the
context of template. Would someone explain it please?

Thanks
Aug 6 '08 #1
2 2000
puzzlecracker wrote:
See it a lot but haven't learn the difference between this two in the
context of template. Would someone explain it please?
Would an example suffice?

template<class Tstruct BT { int dependent; };

struct B { int nondependent; };

template<class Tstruct D1 : BT<T{
void foo() { dependent = 42; } // error!!!!
void bar() { this->dependent = 42; } // OK
};

template<class Tstruct D2 : B {
void foo() { nondependent = 42; } // OK
void bar() { this->nondependent = 42; } // OK
};

int main() {
D1<intd1;
D2<intd2;
}

BTW, have you read the FAQ?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 6 '08 #2
On Aug 6, 11:04 pm, puzzlecracker <ironsel2...@gmail.comwrote:
See it a lot but haven't learn the difference between this
two in the context of template. Would someone explain it
please?
It isn't possible to explain it fully in the context of an
article in news; I'd suggest that you get "C++ Templates: the
Complete Guide", by Vandevoore and Josuttis, and study that.
Basically, however: if a name appears in a context that depends
on one of the template arguments (determined by a fairly complex
set of rules), it is considered dependent (unless it is defined
locally, in the template itself). Dependent names are not bound
(the name isn't associated with the entity it refers to) until
instantiation, at which point, the name is looked up twice: once
in the context where the template was defined, and once, using
argument dependent lookup only, in the context where it was
instantiated. The compiler then decides how to bind the name
(or declare the use ambiguous) according to the declarations it
finds from both lookups (again, determined by a fairly complex
set of rules).

The important points to remember is that if you want the name to
be bound at the point of instantiation, you must do something to
make it dependent. In a function call, the name of a function
is dependent if any of its arguments depend on the template
parameters; for other things, and functions without arguments,
you must find some other hack^H^H^H^Hsolution---if a class
template has a dependent base, for example, the use of this->
makes the name to the right of the -dependent (but limits
lookup to the bases). The other important point to remember is
that if the context in which the name appears isn't dependent,
lookup and binding will occur at the point of definition of the
template, and only at the point of definition. If the name
isn't visible there, it is an error. (What this really means is
that when you get an undefined symbol in a template definition,
the first thing to ask yourself is whether you meant for the
name to be dependent, and if so, find some sort of hack to make
it dependent.)

--
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
Aug 7 '08 #3

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

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.