Template construction in old gcc 3.3.3 does not compile in gcc 3.4.4

Discussion in 'C++' started by eknecronzontas@yahoo.com, Sep 16, 2005.

  1. Guest

    Hello!

    Below is a snippet that compiles in 3.3.3, but not in 3.4.4.
    Obviously, this is a result of the (laudable) efforts to make gcc more
    standards-compliant. Any way to get around this? gcc 3.4.4 complains
    that x is undeclared in the template definition.

    Thanks,
    Andrew Steiner

    -----------------------------------------------------------------------

    #include <iostream>

    using namespace std;

    class parent {
    public:
    parent() { x=3.0; };
    double x;
    };

    template<class parent_t> class child : public parent_t {
    public:
    void test() {
    cout << x << endl;
    }
    };

    int main(void) {
    child<parent> c;
    c.test();

    return 0;
    }
     
    , Sep 16, 2005
    #1
    1. Advertising

  2. eknecronzontas wrote:

    > Below is a snippet that compiles in 3.3.3, but not in 3.4.4.
    > Obviously, this is a result of the (laudable) efforts to make gcc more
    > standards-compliant. Any way to get around this? gcc 3.4.4 complains
    > that x is undeclared in the template definition.
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class parent {
    > public:
    > parent() { x=3.0; };
    > double x;
    > };
    >
    > template<class parent_t> class child : public parent_t {
    > public:
    > void test() {
    > cout << x << endl;


    Change the above line to: cout << this->x << endl;
    and all should work as expected.

    > }
    > };
    >
    > int main(void) {
    > child<parent> c;
    > c.test();
    >
    > return 0;
    > }


    Best regards,

    Tom
     
    Thomas Tutone, Sep 16, 2005
    #2
    1. Advertising

  3. "Thomas Tutone" <> wrote in message
    news:...
    > eknecronzontas wrote:
    >> template<class parent_t> class child : public parent_t {
    >> public:
    >> void test() {
    >> cout << x << endl;

    >
    > Change the above line to: cout << this->x << endl;
    > and all should work as expected.


    parent_t::x works too...

    Ali
     
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Sep 16, 2005
    #3
  4. Guest

    Hehe...should have thought of that. It seems to me that
    parent_t::x should be better, as it doesn't require the
    dereferencing of the pointer....

    Thanks,
    Andrew
     
    , Sep 16, 2005
    #4
  5. eknecronzontas wrote:

    >>>> template<class parent_t> class child : public parent_t {
    >>>> public:
    >>>> void test() {
    >>>> cout << x << endl;


    >>> Change the above line to: cout << this->x << endl;
    >>> and all should work as expected.


    >> parent_t::x works too...


    > Hehe...should have thought of that. It seems to me that
    > parent_t::x should be better, as it doesn't require the
    > dereferencing of the pointer....


    Actually, the two are functionally identical and should result in
    identical compiled code. In both cases you're dereferencing the
    pointer - the first time explicitly, the second time implicitly, but it
    happens either way.

    Best regards,

    Tom
     
    Thomas Tutone, Sep 16, 2005
    #5
  6. Nathan Addy Guest

    I ported some gcc 3.3 code over to gcc 4.0 for the first time last week
    and had this exact same problem. Copying and pasting the relevant bits
    from the gcc 3.4 release page at
    http://gcc.gnu.org/gcc-3.4/changes.html:
    ======================================================
    In a template definition, unqualified names will no longer find members
    of a dependent base (as specified by [temp.dep]/3 in the C++ standard).
    For example,

    template <typename T> struct B {
    int m;
    int n;
    int f ();
    int g ();
    };
    int n;
    int g ();
    template <typename T> struct C : B<T> {
    void h ()
    {
    m = 0; // error
    f (); // error
    n = 0; // ::n is modified
    g (); // ::g is called
    }
    };

    You must make the names dependent, e.g. by prefixing them with this->.
    Here is the corrected definition of C<T>::h,

    template <typename T> void C<T>::h ()
    {
    this->m = 0;
    this->f ();
    this->n = 0
    this->g ();
    }
    ==============================================

    Best,
    Nathan
     
    Nathan Addy, Sep 17, 2005
    #6
    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. frankg
    Replies:
    4
    Views:
    417
    Joe Durusau
    Jul 28, 2003
  2. Ook
    Replies:
    10
    Views:
    584
  3. Replies:
    9
    Views:
    303
  4. Nagaraj
    Replies:
    1
    Views:
    920
    Lionel B
    Mar 1, 2007
  5. fAnSKyer
    Replies:
    2
    Views:
    568
    Alf P. Steinbach
    Jun 7, 2009
Loading...

Share This Page