Nondependent Base Classes

L

lizhuo

hi all:
I reading "C++ Templates: The Complete Guide "
Part II: Templates in Depth
9.4.1 Nondependent Base Classes
he write$B!'(B
In a class template, a nondependent base class is one with a complete
type that can be determined without knowing the template arguments.

who can tell me ,what is "Nondependent Base Classes" ?
if modify line005 "typedef X T;" ,what will happen?

001 template<typename X>
002 class Base {
003 public:
004 int basefield;
005 typedef int T;
006 };
007 template<typename T>
008 class D2 : public Base<double> { // nondependent base
009 public:
010 ......
011 };

013 template<typename T>
014 class DD : public Base<T> { // dependent base
015 public:
016 ......
017 };
 
V

Victor Bazarov

lizhuo said:
hi all:
I reading "C++ Templates: The Complete Guide "
Part II: Templates in Depth
9.4.1 Nondependent Base Classes
he write$B!'(B
In a class template, a nondependent base class is one with a complete
type that can be determined without knowing the template arguments.

who can tell me ,what is "Nondependent Base Classes" ?

Why do you need anybody to tell you? You just wrote the definition,
right there, before your question. What part of it do you not
understand?
if modify line005 "typedef X T;" ,what will happen?

'T' will become a dependent name in Base.
001 template<typename X>
002 class Base {
003 public:
004 int basefield;
005 typedef int T;
006 };
007 template<typename T>
008 class D2 : public Base<double> { // nondependent base
009 public:
010 ......
011 };

013 template<typename T>
014 class DD : public Base<T> { // dependent base
015 public:
016 ......
017 };

V
 
I

Ivan Novick

lizhuo said:
hi all:
I reading "C++ Templates: The Complete Guide "
Part II: Templates in Depth
9.4.1 Nondependent Base Classes
he write$B!'(B
In a class template, a nondependent base class is one with a complete
type that can be determined without knowing the template arguments.

who can tell me ,what is "Nondependent Base Classes" ?
if modify line005 "typedef X T;" ,what will happen?

001 template<typename X>
002 class Base {
003 public:
004 int basefield;
005 typedef int T;
006 };
007 template<typename T>
008 class D2 : public Base<double> { // nondependent base
009 public:
010 ......
011 };

013 template<typename T>
014 class DD : public Base<T> { // dependent base
015 public:
016 ......
017 };

This was discussed recently, and there is an FAQ about it too to look
at it.

Here is sample code that may help:

#include <iostream>
void print_me()
{
std::cout << "global function" << std::endl;
}

template <typename T>
struct wrapper
{
void print_me()
{ std::cout << "base member function" << std::endl; }
};

template <typename T>
struct derived : public wrapper<T>
{
derived()
{
print_me(); // calls global function (correct) with
gcc 4.1
// calls wrapper<T>::print_me
(non-compliant) with gcc 3.2

this->print_me(); // calls wrapper<T>::print_me
wrapper<T>::print_me(); // calls wrapper<T>::print_me
derived<T>::print_me(); // calls wrapper<T>::print_me
}
};

int main(int argc, char** argv)
{
derived<int> x;
return 0;
}
 
H

huangshan

hi :

I just want to know what :

A
> 001 template<typename X>
> 002 class Base {
> 003 public:
> 004 int basefield;
> 005 typedef int T;
> 006 };
in the class Base,the X is not be used,
so it is a Nondependent Base Classes.
(dependent by itself)
or
B
> 008 class D2 : public Base<double> { // nondependent base
> 009 public:
> 010 ......
> 011 };
in line008 ,public Base<double>,
so it is a Nondependent Base Classes.
(dependent by derivation class)


which is right?
 
V

Victor Bazarov

huangshan said:
I just want to know what :

A
in the class Base,the X is not be used,
so it is a Nondependent Base Classes.

It has nothing to do with whether 'X' is used.
(dependent by itself)

What do you mean by "by itself"?

It's non-dependent because, if D2 is a template, 'Base' is not
instantiated with D2's template argument.
in line008 ,public Base<double>,
so it is a Nondependent Base Classes.
(dependent by derivation class)


which is right?

I am not sure I answered your question, but here are the two
examples, first does not have a dependent base class, the second
does:

(1)
template<class T> class B {};
template<class T> class D : public B<void> {};
(2)
template<class T> class B {};
template<class T> class D : public B<T> {};

V
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,177
Latest member
OrderGlucea
Top