access private member function through a function having template type

Discussion in 'C++' started by Mike, Dec 27, 2006.

  1. Mike

    Mike Guest

    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 T> void 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
    Mike, Dec 27, 2006
    #1
    1. Advertising

  2. Mike

    Mike Wahler Guest

    "Mike" <> wrote in message
    news:...
    > 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 T> void 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
    Mike Wahler, Dec 28, 2006
    #2
    1. Advertising

  3. Mike

    Yahooooooooo Guest

    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" <> wrote in message
    > news:...
    > > 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 T> void 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
    Yahooooooooo, Dec 28, 2006
    #3
  4. Mike

    gangs Guest

    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 T> void 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?
    gangs, Dec 28, 2006
    #4
  5. Mike

    Mike Guest

    Mike Wahler wrote:
    > "Mike" <> wrote in message
    > news:...
    > > 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 T> void 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
    Mike, Dec 28, 2006
    #5
  6. Mike

    Mike Guest

    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" <> wrote in message
    > > news:...
    > > > 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 T> void 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
    Mike, Dec 28, 2006
    #6
  7. Mike

    Mike Guest

    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 T> void 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.
    Mike, Dec 28, 2006
    #7
  8. Mike

    BobR Guest

    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 T> class A{ T t[100000]; };
    template<class T> class B{ T t[100000]; };
    template<class T> class C{ T t[100000]; };
    template<class T> class D{ T t[100000]; };
    template<class T> class E{ T t[100000]; };
    // ..... etc. ....(skip class T{};)
    template<class T> class Z{ T t[100000]; };

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

    How much did the executable grow?
    Then change main:

    int main(){
    A<int> Aint;
    B<int> Bint;
    // ... etc.
    Z<int> Zint;
    std::cout<<"Hello World";
    }

    Now how much did the executable grow?

    --
    Bob R
    POVrookie
    BobR, Dec 28, 2006
    #8
  9. Mike

    Taran Guest

    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 T> void 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 T> void 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
    Taran, Dec 29, 2006
    #9
  10. Mike

    Mike Guest

    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 T> void 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 T> void 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
    Mike, Dec 29, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    268
    Victor Bazarov
    Oct 19, 2005
  2. dragoncoder
    Replies:
    31
    Views:
    925
    Vladimir Lushnikov
    Apr 15, 2006
  3. Replies:
    2
    Views:
    512
    Salt_Peter
    Feb 17, 2008
  4. Hicham Mouline
    Replies:
    1
    Views:
    352
    Vladyslav Lazarenko
    Mar 27, 2009
  5. ittium
    Replies:
    5
    Views:
    443
    88888 Dihedral
    Jan 12, 2012
Loading...

Share This Page