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 via email to