[Bug c++/60201] New: Issue with CRTP generation under 4.8.1

2014-02-14 Thread andrew.stern at itg dot com
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 

struct ParamOne {
double val {0.0};
};

struct ParamTwo {
int val {0};
};

template
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 using pdata_type = typename
P::data_type;
template using pother_type =
typename P::other_type;

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

return Subclass.SubclassMethod( data, other );
}

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

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

public:
int m_SubClassValue { 472 };
};

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

template<>
bool Subclass::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, ParamOne, ParamTwo> test;

test.Method(one);
return 0;
}


[Bug c++/60201] Issue with CRTP generation under 4.8.1

2014-02-14 Thread andrew.stern at itg dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60201

--- Comment #2 from Andrew Stern  ---
>>Is this valid as *this at this point is just a Baseclass type (and does not 
>>have a Subclass type.

I believe the code to be valid since the template generates both the Baseclass
and Subclass.  The Subclass object is really just the same object as the
Baseclass and the sizeof operator seems to give the correct values.  Note that
the code compiles and runs and the correct member functions get called.  If you
think this is the issue what is the correct method to get the Subclass type.

Note the line also is consistent with the code located at:
http://eli.thegreenplace.net/2011/05/17/the-curiously-recurring-template-pattern-in-c/

Specifically:const Derived& d1 = static_cast(o1);


[Bug c++/60201] Issue with CRTP generation under 4.8.1

2014-02-14 Thread andrew.stern at itg dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60201

--- Comment #4 from Andrew Stern  ---
Since the examples that I found for CRTP seem to indicate a line very much like
this could you perhaps suggest the correct method to get and call the Subclass
object from the Baseclass object?


[Bug c++/60201] Issue with CRTP generation under 4.8.1

2014-02-14 Thread andrew.stern at itg dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60201

--- Comment #5 from Andrew Stern  ---
It seems that if I change my code
From:
Baseclass, ParamOne, ParamTwo> test;

To:
Subclass test;

Then it seems to work.

This produces the following output:
   The this pointer is 7fffddafc360 with a size of 8 and values of 304 and 472


[Bug c++/60201] Issue with CRTP generation under 4.8.1

2014-02-14 Thread andrew.stern at itg dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60201

--- Comment #6 from Andrew Stern  ---
Thank you for your help.