By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,495 Members | 1,298 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,495 IT Pros & Developers. It's quick & easy.

access private member function through a function having template type

P: n/a
Hi,

Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?

For example:

#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};

template <typename Tvoid goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Why does the compiler not emit error in function "goo" ?

Thanks.

Mike

Dec 28 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a

"Mike" <go*********@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.com...
Hi,

Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?

For example:

#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};

template <typename Tvoid goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Why does the compiler not emit error in function "goo" ?
Because if a templated function is never called, no code is created
for it. Try calling it, and you should get an error to the
effect of: "cannot access private member declared in class 'Testable'"

Note that first you'll need to declare 'Testable::pp()' as a const
member function, or change 'goo()'s parameter 'lhs' to nonconst;
otherwise you'll also get an error about "cannot convert 'this' pointer
from 'const Testable' to 'Testable &'"

-Mike
Dec 28 '06 #2

P: n/a
Before accessing private member fucntion of a class , can you please
read what the PRIVATE word implies...

You cannot access private members fuctions other than same class PUBLIC
member functions.
So no question of private members functions access from template
fuctnions.

spent half a day to know this when i was learning...C++

:)

Regards,
M.Azmath
"Evergreen C++"

Mike Wahler wrote:
"Mike" <go*********@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.com...
Hi,

Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?

For example:

#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};

template <typename Tvoid goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Why does the compiler not emit error in function "goo" ?

Because if a templated function is never called, no code is created
for it. Try calling it, and you should get an error to the
effect of: "cannot access private member declared in class 'Testable'"

Note that first you'll need to declare 'Testable::pp()' as a const
member function, or change 'goo()'s parameter 'lhs' to nonconst;
otherwise you'll also get an error about "cannot convert 'this' pointer
from 'const Testable' to 'Testable &'"

-Mike
Dec 28 '06 #3

P: n/a

Mike wrote:
Hi,

Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?

For example:

#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};

template <typename Tvoid goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Why does the compiler not emit error in function "goo" ?

Thanks.

Mike
Certainly not compiling for me even in the templatized version. I used
VC6 and DevC++ 4.9.9.2 (using mingw32 gcc version 3.4.2 ), in both
cases I got the error at compilation time.
Which compiler are you using?

Dec 28 '06 #4

P: n/a

Mike Wahler wrote:
"Mike" <go*********@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.com...
Hi,

Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?

For example:

#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};

template <typename Tvoid goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Why does the compiler not emit error in function "goo" ?

Because if a templated function is never called, no code is created
for it. Try calling it, and you should get an error to the
effect of: "cannot access private member declared in class 'Testable'"
Thanks for answering.

Yes. The error message does show when I use "goo". Then I am
interested in how the compiler does it.

I guess the compiler seems to tolerate the "lhs.pp()" when scanning the
function body of "goo". But when it sees the invocation of "goo", it
goes back and does the semantic checking. If that's the case, is it
inefficient ?

>
Note that first you'll need to declare 'Testable::pp()' as a const
member function, or change 'goo()'s parameter 'lhs' to nonconst;
otherwise you'll also get an error about "cannot convert 'this' pointer
from 'const Testable' to 'Testable &'"

-Mike
Dec 28 '06 #5

P: n/a

Yahooooooooo wrote:
Before accessing private member fucntion of a class , can you please
read what the PRIVATE word implies...

You cannot access private members fuctions other than same class PUBLIC
member functions.
So no question of private members functions access from template
fuctnions.
Hi, Yahooooooooo,

I don't think your answer is correct. Please see Mike Wahler 's message
above.

Regards,

Mike
>
spent half a day to know this when i was learning...C++

:)

Regards,
M.Azmath
"Evergreen C++"

Mike Wahler wrote:
"Mike" <go*********@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.com...
Hi,
>
Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?
>
For example:
>
#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};
>
template <typename Tvoid goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}
>
void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}
>
Why does the compiler not emit error in function "goo" ?
Because if a templated function is never called, no code is created
for it. Try calling it, and you should get an error to the
effect of: "cannot access private member declared in class 'Testable'"

Note that first you'll need to declare 'Testable::pp()' as a const
member function, or change 'goo()'s parameter 'lhs' to nonconst;
otherwise you'll also get an error about "cannot convert 'this' pointer
from 'const Testable' to 'Testable &'"

-Mike
Dec 28 '06 #6

P: n/a

gangs wrote:
Mike wrote:
Hi,

Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?

For example:

#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};

template <typename Tvoid goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Why does the compiler not emit error in function "goo" ?

Thanks.

Mike

Certainly not compiling for me even in the templatized version. I used
VC6 and DevC++ 4.9.9.2 (using mingw32 gcc version 3.4.2 ), in both
cases I got the error at compilation time.
Which compiler are you using?
I used g++(3.2.2) on red hat linux.

Dec 28 '06 #7

P: n/a

Mike wrote in message...
>
I guess the compiler seems to tolerate the "lhs.pp()" when scanning the
function body of "goo". But when it sees the invocation of "goo", it
goes back and does the semantic checking. If that's the case, is it
inefficient ?
You are trying to compare a blueprint of a house (the class template) with a
house (the instantiated class).

Make a program:

#include <iostream>
int main(){
std::cout<<"Hello World";
}

Compile and look at the executable's size. Write it down.
Now do this:

#include <iostream>
template<class Tclass A{ T t[100000]; };
template<class Tclass B{ T t[100000]; };
template<class Tclass C{ T t[100000]; };
template<class Tclass D{ T t[100000]; };
template<class Tclass E{ T t[100000]; };
// ..... etc. ....(skip class T{};)
template<class Tclass Z{ T t[100000]; };

int main(){
std::cout<<"Hello World";
}

How much did the executable grow?
Then change main:

int main(){
A<intAint;
B<intBint;
// ... etc.
Z<intZint;
std::cout<<"Hello World";
}

Now how much did the executable grow?

--
Bob R
POVrookie
Dec 28 '06 #8

P: n/a
Mike wrote:
Hi,

Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?

For example:

#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};

template <typename Tvoid goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Why does the compiler not emit error in function "goo" ?

Thanks.

Mike
Templates are like preprocessor macros. During preprocessing the
template code is intantiated for each type of the template type, this
is called instantitation. So even if you have a template which has a
erronoeus code, as you have in here. the compiler will not complain if
your template is not intantiated. Simple put non instantiated template
code does not exist.
#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};
template <typename Tvoid goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}
The compier will complain only when you instantiate this template
function
E.g

void goo<int>(Testbale_object, some_inv_value);
//The compiler will complain now as the template with error has been
intantiated.

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}
Expected.
Why does the compiler not emit error in function "goo" ?
The template was instantiated and hence the code does not exist during
compilation.
A word of caution, since templates are intantiated for each type you
specify, this is a cause of code bloat. You may to read more about
this.

- HTH

Regards,
Taran

Dec 29 '06 #9

P: n/a

Taran wrote:
Mike wrote:
Hi,

Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?

For example:

#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};

template <typename Tvoid goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Why does the compiler not emit error in function "goo" ?

Thanks.

Mike

Templates are like preprocessor macros. During preprocessing the
template code is intantiated for each type of the template type, this
is called instantitation. So even if you have a template which has a
erronoeus code, as you have in here. the compiler will not complain if
your template is not intantiated. Simple put non instantiated template
code does not exist.
Good. That makes sense. Thanks.

>
#include<iostream>
using namespace std;
class Testable {
void pp() {} // a private function
};
template <typename Tvoid goo(const Testable& lhs, const T& rhs) {
lhs.pp(); // it is ok ??
}
The compier will complain only when you instantiate this template
function
E.g

void goo<int>(Testbale_object, some_inv_value);
//The compiler will complain now as the template with error has been
intantiated.

void goo2(const Testable& lhs) {
lhs.pp(); // the compiler reports error.
}

Expected.
Why does the compiler not emit error in function "goo" ?

The template was instantiated and hence the code does not exist during
compilation.
A word of caution, since templates are intantiated for each type you
specify, this is a cause of code bloat. You may to read more about
this.

- HTH

Regards,
Taran
Dec 29 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.