public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "andrew.stern at itg dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/60201] New: Issue with CRTP generation under 4.8.1
Date: Fri, 14 Feb 2014 17:46:00 -0000	[thread overview]
Message-ID: <bug-60201-4@http.gcc.gnu.org/bugzilla/> (raw)

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60201

            Bug ID: 60201
           Summary: Issue with CRTP generation under 4.8.1
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andrew.stern at itg dot com

I created a CRTP (Curiously recurring template pattern) and added non-static
member variables to my base class and that works without issue.  But when I add
non-static variables to the subclass instance the initialization and values for
the variables in this class don't get initialized properly and also don't
consistently keep the same values.  I checked the size-of the object and it
does seem to contain the correct size that is the Base member variables plus
the Subclasses member variables and the this pointer is consistently the same
value.  It's just that when dumping the values to the screen the ones in the
subclass multiple instances of my object are dumping the same values regardless
of the object. I'm guessing that that the computation of the offset in the
object for these variables is being calculated incorrectly.  Note that 472
should have been printed and not 0 in the example below.


gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-2/root/usr/libexec/gcc/x86_64-redhat-linux/4.8.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/opt/rh/devtoolset-2/root/usr
--mandir=/opt/rh/devtoolset-2/root/usr/share/man
--infodir=/opt/rh/devtoolset-2/root/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap
--enable-shared --enable-threads=posix --enable-checking=release
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--enable-languages=c,c++,fortran,lto --enable-plugin
--with-linker-hash-style=gnu --enable-initfini-array --disable-libgcj
--with-isl=/builddir/build/BUILD/gcc-4.8.1-20130715/obj-x86_64-redhat-linux/isl-install
--with-cloog=/builddir/build/BUILD/gcc-4.8.1-20130715/obj-x86_64-redhat-linux/cloog-install
--with-mpc=/builddir/build/BUILD/gcc-4.8.1-20130715/obj-x86_64-redhat-linux/mpc-install
--with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.1 20130715 (Red Hat 4.8.1-4) (GCC) 

Compile flags:
-g;-Wall;-std=c++11;-O0

To execute: 
~/crtpbug/crtpbug/Debug/crtpbug

Output of application:
The this pointer is 7fff2124ec70 with a size of 8 and values of 304 and 0

Source code:
#include <stdio.h>

struct ParamOne {
    double val {0.0};
};

struct ParamTwo {
    int val {0};
};

template<typename P, typename Data, typename Other>
class Baseclass
{
public:
    using subclass_type = P;
    using data_type = Data;
    using other_type = Other;

    bool Method( const Data &data);

public:
    int m_BaseClassValue { 304 };
};

template<typename P, typename Data, typename Other> using pdata_type = typename
P::data_type;
template<typename P, typename Data, typename Other> using pother_type =
typename P::other_type;

template<typename P, typename Data, typename Other>
bool Baseclass<P, Data, Other>::Method( const Data &data )
{
    P& Subclass = static_cast<P&>( *this );
    pother_type<P, Data, Other> other;
    other.val = 11;

    return Subclass.SubclassMethod( data, other );
}

template<typename Data, typename Other>
class Subclass : public Baseclass<Subclass<Data, Other>, Data, Other>
{
public:
    using data_type = Data;
    using other_type = Other;

    bool SubclassMethod( const Data &data, Other &other );

public:
    int m_SubClassValue { 472 };
};

template<typename Data, typename Other>
bool Subclass<Data, Other>::SubclassMethod( const Data &data, Other &other )
{
    return true;
}

template<>
bool Subclass<ParamOne, ParamTwo>::SubclassMethod( const ParamOne &data,
ParamTwo &other )
{
    printf( "The this pointer is %lx with a size of %ld and values of %d and
%d\n", (long)this, sizeof(*this), m_BaseClassValue, m_SubClassValue );
    return true;
}

int main(int argc, char **argv)
{
    ParamOne one;
    one.val = 5.0;

    Baseclass<Subclass<ParamOne, ParamTwo>, ParamOne, ParamTwo> test;

    test.Method(one);
    return 0;
}


             reply	other threads:[~2014-02-14 17:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-14 17:46 andrew.stern at itg dot com [this message]
2014-02-14 19:20 ` [Bug c++/60201] " pinskia at gcc dot gnu.org
2014-02-14 19:30 ` andrew.stern at itg dot com
2014-02-14 19:44 ` pinskia at gcc dot gnu.org
2014-02-14 20:00 ` andrew.stern at itg dot com
2014-02-14 20:07 ` andrew.stern at itg dot com
2014-02-14 20:08 ` andrew.stern at itg dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-60201-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).