D
danilo.turina
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::Userlabel> p;
}
-----------------------------------------
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::Userlabel> p;
}
-----------------------------------------
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::Userlabel> p;
to:
SchemaBasedIdToLabelMapper<Customer, reinterpret_cast<const int
Customer::*>(&Customer::Id), &Customer::Userlabel> p;
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
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::Userlabel> p;
}
-----------------------------------------
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::Userlabel> p;
}
-----------------------------------------
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::Userlabel> p;
to:
SchemaBasedIdToLabelMapper<Customer, reinterpret_cast<const int
Customer::*>(&Customer::Id), &Customer::Userlabel> p;
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