This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Issue with CRTP generation under 4.8.1


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 sizeof 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 values. 
It's just that when dumping the values to the screen the ones in the
subclass aren't the values that I'm expecting within my template specialization
methods of the subclass.  Since multiple
instances of my object are dumping the same values regardless of the object I'm
guessing 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.

 

Example of bug:

 

#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;

}

 

Output is:

 

The this pointer is 7fffc4e87670 with a size of 8 and
values of 304 and 0. 

 

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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]