473,466 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Non-type template arguments and inheritance

Hi all,
today I encountered a problem that I'm only able to solve by using
reinterpret_cast (and I'd like to avoid it).

This was my code until yesterday (omitting includes, "using namespace"
and the like):

-----------------------------------------
// ABC Table
struct Table {
Table(const string& name);

string name;
};

Table::Table(const string& name) : name(name) {
cout << name << endl;
}

// Class Customer
struct Customer : public Table {
const int Id;
const int Userlabel;

Customer();
};

Customer::Customer() : Table("Customer"), Id(1), Userlabel(2) {
}

// Class SchemaBasedIdToLabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdToLabelMapper {
public:
SchemaBasedIdToLabelMapper();
};

template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
cout << (void *) this << endl;
C c;
cout << "IdColumn: " << c.*idColumn << endl;
cout << "LabelColumn: " << c.*labelColumn << endl;
}

int main() {
SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;
}
-----------------------------------------

It worked like a charm. No problem at all.

Today, I had to make a change in the class hierarchy so that Customer
would no more inherit directly from Table but, instead, there would be
an intermediate class between the two that had one of the const
members of Customer.

Here is the new code:

-----------------------------------------
// ABC Table
struct Table {
Table(const string& name);

string name;
};

Table::Table(const string& name) : name(name) {
cout << name << endl;
}

// Class TableWithId (intermediate class)
struct TableWithId : public Table {
const int Id;

TableWithId(const string& name, int id);
};

TableWithId::TableWithId(const string& name, int id) : Table(name),
Id(id) {
}
// Class Customer
struct Customer : public TableWithId {
const int Userlabel;

Customer();
};

Customer::Customer() : TableWithId("Customer", 1), Userlabel(2) {
}

// Class SchemaBasedIdToLabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdToLabelMapper {
public:
SchemaBasedIdToLabelMapper();
};

template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
cout << (void *) this << endl;
C c;
cout << "IdColumn: " << c.*idColumn << endl;
cout << "LabelColumn: " << c.*labelColumn << endl;
}

int main() {
SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;
}
-----------------------------------------

The code above does not compile, the error that I receive is:

# Non-type template arguments may not create temporaries.

The problem (as I understand it) is that &Customer::Id in reality is
&TableWithId::Id that is of type (typeid() told me) "int const
TableWithId::*" and, since the template requires that the passed
parameter is of type "int const Customer::*", a conversion is
required. This conversion is implemented by the compiler using a
temporary and this, of course, cannot be used with a template
definition.

I tried to change the template, but I don't want to be obliged to
specified in which ancestor the field I'm interested in is.

The only way to make that code compile is to change:

SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;

to:

SchemaBasedIdToLabelMapper<Customer, reinterpret_cast<const int
Customer::*>(&Customer::Id), &Customer::Userlabelp;

since (for what's my understanding) the reinterpret_cast does not
create a temporary for the cast (since it's not needed...).

The code compiles and runs fine (is this foreseen by the standard?
with another compiler/platform/weather this could break?). Anyway I'm
not so comfortable with reinterpret_cast so I'll be very happy if
there were some other solution to this issue.

Is there a way to solve this problem without using reinterpret_cast?

Ciao,
Danilo
Aug 8 '08 #1
4 1715
On Aug 8, 9:35*am, danilo.tur...@gmail.com wrote:
* * today I encountered a problem that I'm only able to solve by using
reinterpret_cast (and I'd like to avoid it).

This was my code until yesterday (omitting includes, "using namespace"
and the like):

-----------------------------------------
// ABC Table
struct Table {
* * Table(const string& name);

* * string name;

};

Table::Table(const string& name) : name(name) {
* * cout << name << endl;

}

// Class Customer
struct Customer : public Table {
* * const int Id;
* * const int Userlabel;

* * Customer();

};

Customer::Customer() : Table("Customer"), Id(1), Userlabel(2) {

}

// Class SchemaBasedIdToLabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdToLabelMapper {
public:
* * SchemaBasedIdToLabelMapper();

};

template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColumn: " << c.*labelColumn << endl;

}

int main() {
* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;}

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

It worked like a charm. No problem at all.

Today, I had to make a change in the class hierarchy so that Customer
would no more inherit directly from Table but, instead, there would be
an intermediate class between the two that had one of the const
members of Customer.

Here is the new code:

-----------------------------------------
// ABC Table
struct Table {
* * Table(const string& name);

* * string name;

};

Table::Table(const string& name) : name(name) {
* * cout << name << endl;

}

// Class TableWithId (intermediate class)
struct TableWithId : public Table {
* * const int Id;

* * TableWithId(const string& name, int id);

};

TableWithId::TableWithId(const string& name, int id) : Table(name),
Id(id) {

}

// Class Customer
struct Customer : public TableWithId {
* * const int Userlabel;

* * Customer();

};

Customer::Customer() : TableWithId("Customer", 1), Userlabel(2) {

}

// Class SchemaBasedIdToLabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdToLabelMapper {
public:
* * SchemaBasedIdToLabelMapper();

};

template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColumn: " << c.*labelColumn << endl;

}

int main() {
* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;}

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

The code above does not compile, the error that I receive is:

# Non-type template arguments may not create temporaries.

The problem (as I understand it) is that &Customer::Id in reality is
&TableWithId::Id that is of type (typeid() told me) "int const
TableWithId::*" and, since the template requires that the passed
parameter is of type "int const Customer::*", a conversion is
required. This conversion is implemented by the compiler using a
temporary and this, of course, cannot be used with a template
definition.

I tried to change the template, but I don't want to be obliged to
specified in which ancestor the field I'm interested in is.

The only way to make that code compile is to change:

* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;

to:

* * SchemaBasedIdToLabelMapper<Customer, reinterpret_cast<const int
Customer::*>(&Customer::Id), &Customer::Userlabelp;

since (for what's my understanding) the reinterpret_cast does not
create a temporary for the cast (since it's not needed...).

The code compiles and runs fine (is this foreseen by the standard?
with another compiler/platform/weather this could break?). Anyway I'm
not so comfortable with reinterpret_cast so I'll be very happy if
there were some other solution to this issue.

Is there a way to solve this problem without using reinterpret_cast?
Your reinterpret_cast solution is non-standard and does not work with
EDG or MINGW (courtesy of Dinkumware.com) or Comeau's online tests,
and the last is usually the most standard compliant compiler around.
How about duplicating the member in the subclass like this:

// Class Customer
struct Customer : public TableWithId {
const int Userlabel;
const int Id; // Note this line

Customer::Customer()
: TableWithId("Customer", 1)
, Userlabel(2)
, Id(TableWithId::Id) // Note this line
{}
};

Works for me on all the aforementioned compilers and VS2005.

Ciao! --M
Aug 8 '08 #2
On 8 Ago, 19:57, mlimber <mlim...@gmail.comwrote:
On Aug 8, 9:35*am, danilo.tur...@gmail.com wrote:
* * today I encountered a problem that I'm only able to solve by using
reinterpret_cast(and I'd like to avoid it).
This was my code until yesterday (omitting includes, "using namespace"
and the like):
-----------------------------------------
// ABC Table
struct Table {
* * Table(const string& name);
* * string name;
};
Table::Table(const string& name) : name(name) {
* * cout << name << endl;
}
// Class Customer
struct Customer : public Table {
* * const int Id;
* * const int Userlabel;
* * Customer();
};
Customer::Customer() : Table("Customer"), Id(1), Userlabel(2) {
}
// Class SchemaBasedIdToLabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdToLabelMapper {
public:
* * SchemaBasedIdToLabelMapper();
};
template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColumn: " << c.*labelColumn << endl;
}
int main() {
* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;}
-----------------------------------------
It worked like a charm. No problem at all.
Today, I had to make a change in the class hierarchy so that Customer
would no more inherit directly from Table but, instead, there would be
an intermediate class between the two that had one of the const
members of Customer.
Here is the new code:
-----------------------------------------
// ABC Table
struct Table {
* * Table(const string& name);
* * string name;
};
Table::Table(const string& name) : name(name) {
* * cout << name << endl;
}
// Class TableWithId (intermediate class)
struct TableWithId : public Table {
* * const int Id;
* * TableWithId(const string& name, int id);
};
TableWithId::TableWithId(const string& name, int id) : Table(name),
Id(id) {
}
// Class Customer
struct Customer : public TableWithId {
* * const int Userlabel;
* * Customer();
};
Customer::Customer() : TableWithId("Customer", 1), Userlabel(2) {
}
// Class SchemaBasedIdToLabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdToLabelMapper {
public:
* * SchemaBasedIdToLabelMapper();
};
template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColumn: " << c.*labelColumn << endl;
}
int main() {
* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;}
-----------------------------------------
The code above does not compile, the error that I receive is:
# Non-type template arguments may not create temporaries.
The problem (as I understand it) is that &Customer::Id in reality is
&TableWithId::Id that is of type (typeid() told me) "int const
TableWithId::*" and, since the template requires that the passed
parameter is of type "int const Customer::*", a conversion is
required. This conversion is implemented by the compiler using a
temporary and this, of course, cannot be used with a template
definition.
I tried to change the template, but I don't want to be obliged to
specified in which ancestor the field I'm interested in is.
The only way to make that code compile is to change:
* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;
to:
* * SchemaBasedIdToLabelMapper<Customer,reinterpret_ca st<const int
Customer::*>(&Customer::Id), &Customer::Userlabelp;
since (for what's my understanding) thereinterpret_castdoes not
create a temporary for the cast (since it's not needed...).
The code compiles and runs fine (is this foreseen by the standard?
with another compiler/platform/weather this could break?). Anyway I'm
not so comfortable withreinterpret_castso I'll be very happy if
there were some other solution to this issue.
Is there a way to solve this problem without usingreinterpret_cast?

Your reinterpret_castsolution is non-standard and does not work with
EDG or MINGW (courtesy of Dinkumware.com) or Comeau's online tests,
and the last is usually the most standard compliant compiler around.
I suspected that. Thanks for confirming.
How about duplicating the member in the subclass like this:

*// Class Customer
*struct Customer : public TableWithId {
* * const int Userlabel;
* * const int Id; * * * * * // Note this line

* * Customer::Customer()
* * * * : TableWithId("Customer", 1)
* * * * , Userlabel(2)
* * * * , Id(TableWithId::Id) *// Note this line
* * {}
*};

Works for me on all the aforementioned compilers and VS2005.
This works but it's not a solution.

In the example I posted, both Customer and SchemaBasedIdToLabelMapper
are in the same source file, while in reality they are two classes
that belongs to two different libraries.

Customer is static from my point of view (i.e. I cannot change it),
while I can operate on the template. So any modification to Customer
is not a solution for me.

Anyway, just for the records, up to now I wasn't able to find any
solution to this problem.
>
Ciao! --M
Thanks for trying.
Ciao,
Danilo
Aug 27 '08 #3
On 27 Ago, 12:31, danilo.tur...@gmail.com wrote:
On 8 Ago, 19:57, mlimber <mlim...@gmail.comwrote:


On Aug 8, 9:35*am, danilo.tur...@gmail.com wrote:
* * today I encountered a problem that I'm only able to solve by using
>reinterpret_cast(and I'd like to avoid it).
This was my code until yesterday (omitting includes, "using namespace"
and the like):
-----------------------------------------
// ABC Table
struct Table {
* * Table(const string& name);
* * string name;
};
Table::Table(const string& name) : name(name) {
* * cout << name << endl;
}
// Class Customer
struct Customer : public Table {
* * const int Id;
* * const int Userlabel;
* * Customer();
};
Customer::Customer() : Table("Customer"), Id(1), Userlabel(2) {
}
// Class SchemaBasedIdToLabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdToLabelMapper {
public:
* * SchemaBasedIdToLabelMapper();
};
template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColumn: " << c.*labelColumn << endl;
}
int main() {
* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;}
-----------------------------------------
It worked like a charm. No problem at all.
Today, I had to make a change in the class hierarchy so that Customer
would no more inherit directly from Table but, instead, there would be
an intermediate class between the two that had one of the const
members of Customer.
Here is the new code:
-----------------------------------------
// ABC Table
struct Table {
* * Table(const string& name);
* * string name;
};
Table::Table(const string& name) : name(name) {
* * cout << name << endl;
}
// Class TableWithId (intermediate class)
struct TableWithId : public Table {
* * const int Id;
* * TableWithId(const string& name, int id);
};
TableWithId::TableWithId(const string& name, int id) : Table(name),
Id(id) {
}
// Class Customer
struct Customer : public TableWithId {
* * const int Userlabel;
* * Customer();
};
Customer::Customer() : TableWithId("Customer", 1), Userlabel(2) {
}
// Class SchemaBasedIdToLabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdToLabelMapper {
public:
* * SchemaBasedIdToLabelMapper();
};
template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColumn: " << c.*labelColumn << endl;
}
int main() {
* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;}
-----------------------------------------
The code above does not compile, the error that I receive is:
# Non-type template arguments may not create temporaries.
The problem (as I understand it) is that &Customer::Id in reality is
&TableWithId::Id that is of type (typeid() told me) "int const
TableWithId::*" and, since the template requires that the passed
parameter is of type "int const Customer::*", a conversion is
required. This conversion is implemented by the compiler using a
temporary and this, of course, cannot be used with a template
definition.
I tried to change the template, but I don't want to be obliged to
specified in which ancestor the field I'm interested in is.
The only way to make that code compile is to change:
* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;
to:
* * SchemaBasedIdToLabelMapper<Customer,reinterpret_ca st<const int
Customer::*>(&Customer::Id), &Customer::Userlabelp;
since (for what's my understanding) thereinterpret_castdoes not
create a temporary for the cast (since it's not needed...).
The code compiles and runs fine (is this foreseen by the standard?
with another compiler/platform/weather this could break?). Anyway I'm
not so comfortable withreinterpret_castso I'll be very happy if
there were some other solution to this issue.
Is there a way to solve this problem without usingreinterpret_cast?
Your reinterpret_castsolution is non-standard and does not work with
EDG or MINGW (courtesy of Dinkumware.com) or Comeau's online tests,
and the last is usually the most standard compliant compiler around.

I suspected that. Thanks for confirming.
How about duplicating the member in the subclass like this:
*// Class Customer
*struct Customer : public TableWithId {
* * const int Userlabel;
* * const int Id; * * * * * // Note this line
* * Customer::Customer()
* * * * : TableWithId("Customer", 1)
* * * * , Userlabel(2)
* * * * , Id(TableWithId::Id) *// Note this line
* * {}
*};
Works for me on all the aforementioned compilers and VS2005.

This works but it's not a solution.

In the example I posted, both Customer and SchemaBasedIdToLabelMapper
are in the same source file, while in reality they are two classes
that belongs to two different libraries.

Customer is static from my point of view (i.e. I cannot change it),
while I can operate on the template. So any modification to Customer
is not a solution for me.

Anyway, just for the records, up to now I wasn't able to find any
solution to this problem.
Ciao! --M

Thanks for trying.
Ciao,
* * * * * * Danilo- Nascondi testo citato

- Mostra testo citato -
Hi,
interesting... You can try tagging the base class.
The following compiles.
Hope it helps a little.
Bye,
Francesco

#include <iostream>
using namespace std;
// ABC Table
struct Table {
Table(const string& name);
string name;

};
Table::Table(const string& name) : name(name) {
cout << name << endl;
}
// Class TableWithId (intermediate class)
struct TableWithId : public Table {

/*********************/
typedef TableWithId TableWithIDTag;
/*********************/
const int Id;

TableWithId(const string& name, int id);

};
TableWithId::TableWithId(const string& name, int id) : Table(name),
Id(id) {
}
// Class Customer
struct Customer : public TableWithId {
const int Userlabel;

Customer();

};
Customer::Customer() : TableWithId("Customer", 1), Userlabel(2) {
}
// Class SchemaBasedIdToLabelMapper
template<class C, const int C::TableWithId::* idColumn, const int C::*
labelColumn>
class SchemaBasedIdToLabelMapper {
public:
SchemaBasedIdToLabelMapper();
};
template<class C, const int C::TableWithId::* idColumn, const int C::*
labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
cout << (void *) this << endl;
C c;
cout << "IdColumn: " << c.*idColumn << endl;
cout << "LabelColumn: " << c.*labelColumn << endl;
}
int main() {
SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;

}

//END CODE
Aug 27 '08 #4
On 27 Ago, 13:36, Francesco <xtrigger...@gmail.comwrote:
On 27 Ago, 12:31, danilo.tur...@gmail.com wrote:


On 8 Ago, 19:57, mlimber <mlim...@gmail.comwrote:
On Aug 8, 9:35*am, danilo.tur...@gmail.com wrote:
* * today I encountered a problem that I'm only able to solve by using
reinterpret_cast(and I'd like to avoid it).
This was my code until yesterday (omitting includes, "using namespace"
and the like):
-----------------------------------------
// ABC Table
struct Table {
* * Table(const string& name);
* * string name;
};
Table::Table(const string& name) : name(name) {
* * cout << name << endl;
}
// Class Customer
struct Customer : public Table {
* * const int Id;
* * const int Userlabel;
* * Customer();
};
Customer::Customer() : Table("Customer"), Id(1), Userlabel(2) {
}
// Class SchemaBasedIdToLabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdToLabelMapper {
public:
* * SchemaBasedIdToLabelMapper();
};
template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColumn: " << c.*labelColumn << endl;
}
int main() {
* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;}
-----------------------------------------
It worked like a charm. No problem at all.
Today, I had to make a change in the class hierarchy so that Customer
would no more inherit directly from Table but, instead, there wouldbe
an intermediate class between the two that had one of the const
members of Customer.
Here is the new code:
-----------------------------------------
// ABC Table
struct Table {
* * Table(const string& name);
* * string name;
};
Table::Table(const string& name) : name(name) {
* * cout << name << endl;
}
// Class TableWithId (intermediate class)
struct TableWithId : public Table {
* * const int Id;
* * TableWithId(const string& name, int id);
};
TableWithId::TableWithId(const string& name, int id) : Table(name),
Id(id) {
}
// Class Customer
struct Customer : public TableWithId {
* * const int Userlabel;
* * Customer();
};
Customer::Customer() : TableWithId("Customer", 1), Userlabel(2) {
}
// Class SchemaBasedIdToLabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdToLabelMapper {
public:
* * SchemaBasedIdToLabelMapper();
};
template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColumn: " << c.*labelColumn << endl;
}
int main() {
* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;}
-----------------------------------------
The code above does not compile, the error that I receive is:
# Non-type template arguments may not create temporaries.
The problem (as I understand it) is that &Customer::Id in reality is
&TableWithId::Id that is of type (typeid() told me) "int const
TableWithId::*" and, since the template requires that the passed
parameter is of type "int const Customer::*", a conversion is
required. This conversion is implemented by the compiler using a
temporary and this, of course, cannot be used with a template
definition.
I tried to change the template, but I don't want to be obliged to
specified in which ancestor the field I'm interested in is.
The only way to make that code compile is to change:
* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;
to:
* * SchemaBasedIdToLabelMapper<Customer,reinterpret_ca st<const int
Customer::*>(&Customer::Id), &Customer::Userlabelp;
since (for what's my understanding) thereinterpret_castdoes not
create a temporary for the cast (since it's not needed...).
The code compiles and runs fine (is this foreseen by the standard?
with another compiler/platform/weather this could break?). Anyway I'm
not so comfortable withreinterpret_castso I'll be very happy if
there were some other solution to this issue.
Is there a way to solve this problem without usingreinterpret_cast?
Your reinterpret_castsolution is non-standard and does not work with
EDG or MINGW (courtesy of Dinkumware.com) or Comeau's online tests,
and the last is usually the most standard compliant compiler around.
I suspected that. Thanks for confirming.
How about duplicating the member in the subclass like this:
*// Class Customer
*struct Customer : public TableWithId {
* * const int Userlabel;
* * const int Id; * * * * * // Note this line
* * Customer::Customer()
* * * * : TableWithId("Customer", 1)
* * * * , Userlabel(2)
* * * * , Id(TableWithId::Id) *// Note this line
* * {}
*};
Works for me on all the aforementioned compilers and VS2005.
This works but it's not a solution.
In the example I posted, both Customer and SchemaBasedIdToLabelMapper
are in the same source file, while in reality they are two classes
that belongs to two different libraries.
Customer is static from my point of view (i.e. I cannot change it),
while I can operate on the template. So any modification to Customer
is not a solution for me.
Anyway, just for the records, up to now I wasn't able to find any
solution to this problem.
Ciao! --M
Thanks for trying.
Ciao,
* * * * * * Danilo- Nascondi testo citato
- Mostra testo citato -

Hi,
interesting... You can try tagging the base class.
The following compiles.
Hope it helps a little.
Bye,
Francesco

#include <iostream>
using namespace std;

// ABC Table
struct Table {
* * Table(const string& name);

* * string name;

};

Table::Table(const string& name) : name(name) {
* * cout << name << endl;

}

// Class TableWithId (intermediate class)
struct TableWithId : public Table {

* * * * /*********************/
* * * * typedef TableWithId TableWithIDTag;
* * * * /*********************/
* * const int Id;

* * TableWithId(const string& name, int id);

};

TableWithId::TableWithId(const string& name, int id) : Table(name),
Id(id) {

}

// Class Customer
struct Customer : public TableWithId {
* * const int Userlabel;

* * Customer();

};

Customer::Customer() : TableWithId("Customer", 1), Userlabel(2) {

}

// Class SchemaBasedIdToLabelMapper
template<class C, const int C::TableWithId::* idColumn, const int C::*
labelColumn>
class SchemaBasedIdToLabelMapper {
public:
* * SchemaBasedIdToLabelMapper();

};

template<class C, const int C::TableWithId::* idColumn, const int C::*
labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColumn: " << c.*labelColumn << endl;

}

int main() {
* * SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabelp;

}

//END CODE- Nascondi testo citato

- Mostra testo citato -
SOrry, I've posted in a rush. The tagging it's useless and I actually
mistyped it.
What I meant is:
struct A
{
int mNumb;
};
struct B : A
{
};

struct C : B
{};
template< typename T, int T::A::* KPtr >
struct D
{};
int main()
{
D< C, &C::mNumb > obj;
}
Aug 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
13
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.