473,609 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
+ 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_cas t (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(co nst string& name) : name(name) {
cout << name << endl;
}

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

Customer();
};

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

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

template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdTo LabelMapper<C, idColumn,
labelColumn>::S chemaBasedIdToL abelMapper() {
cout << (void *) this << endl;
C c;
cout << "IdColumn: " << c.*idColumn << endl;
cout << "LabelColum n: " << c.*labelColumn << endl;
}

int main() {
SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;
}
-----------------------------------------

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(co nst string& name) : name(name) {
cout << name << endl;
}

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

TableWithId(con st string& name, int id);
};

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

Customer();
};

Customer::Custo mer() : TableWithId("Cu stomer", 1), Userlabel(2) {
}

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

template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdTo LabelMapper<C, idColumn,
labelColumn>::S chemaBasedIdToL abelMapper() {
cout << (void *) this << endl;
C c;
cout << "IdColumn: " << c.*idColumn << endl;
cout << "LabelColum n: " << c.*labelColumn << endl;
}

int main() {
SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;
}
-----------------------------------------

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::I d in reality is
&TableWithId::I d 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:

SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;

to:

SchemaBasedIdTo LabelMapper<Cus tomer, reinterpret_cas t<const int
Customer::*>(&C ustomer::Id), &Customer::User labelp;

since (for what's my understanding) the reinterpret_cas t 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_cas t 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_cas t?

Ciao,
Danilo
Aug 8 '08 #1
4 1725
On Aug 8, 9:35*am, danilo.tur...@g mail.com wrote:
* * today I encountered a problem that I'm only able to solve by using
reinterpret_cas t (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(co nst string& name) : name(name) {
* * cout << name << endl;

}

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

* * Customer();

};

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

}

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

};

template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdTo LabelMapper<C, idColumn,
labelColumn>::S chemaBasedIdToL abelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColum n: " << c.*labelColumn << endl;

}

int main() {
* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;}

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

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(co nst string& name) : name(name) {
* * cout << name << endl;

}

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

* * TableWithId(con st string& name, int id);

};

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

}

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

* * Customer();

};

Customer::Custo mer() : TableWithId("Cu stomer", 1), Userlabel(2) {

}

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

};

template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdTo LabelMapper<C, idColumn,
labelColumn>::S chemaBasedIdToL abelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColum n: " << c.*labelColumn << endl;

}

int main() {
* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;}

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

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::I d in reality is
&TableWithId::I d 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:

* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;

to:

* * SchemaBasedIdTo LabelMapper<Cus tomer, reinterpret_cas t<const int
Customer::*>(&C ustomer::Id), &Customer::User labelp;

since (for what's my understanding) the reinterpret_cas t 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_cas t 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_cas t?
Your reinterpret_cas t 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::Custo mer()
: TableWithId("Cu stomer", 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...@g mail.com wrote:
* * today I encountered a problem that I'm only able to solve by using
reinterpret_cas t(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(co nst string& name) : name(name) {
* * cout << name << endl;
}
// Class Customer
struct Customer : public Table {
* * const int Id;
* * const int Userlabel;
* * Customer();
};
Customer::Custo mer() : Table("Customer "), Id(1), Userlabel(2) {
}
// Class SchemaBasedIdTo LabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdTo LabelMapper {
public:
* * SchemaBasedIdTo LabelMapper();
};
template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdTo LabelMapper<C, idColumn,
labelColumn>::S chemaBasedIdToL abelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColum n: " << c.*labelColumn << endl;
}
int main() {
* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;}
-----------------------------------------
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(co nst string& name) : name(name) {
* * cout << name << endl;
}
// Class TableWithId (intermediate class)
struct TableWithId : public Table {
* * const int Id;
* * TableWithId(con st string& name, int id);
};
TableWithId::Ta bleWithId(const string& name, int id) : Table(name),
Id(id) {
}
// Class Customer
struct Customer : public TableWithId {
* * const int Userlabel;
* * Customer();
};
Customer::Custo mer() : TableWithId("Cu stomer", 1), Userlabel(2) {
}
// Class SchemaBasedIdTo LabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdTo LabelMapper {
public:
* * SchemaBasedIdTo LabelMapper();
};
template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdTo LabelMapper<C, idColumn,
labelColumn>::S chemaBasedIdToL abelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColum n: " << c.*labelColumn << endl;
}
int main() {
* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;}
-----------------------------------------
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::I d in reality is
&TableWithId::I d 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:
* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;
to:
* * SchemaBasedIdTo LabelMapper<Cus tomer,reinterpr et_cast<const int
Customer::*>(&C ustomer::Id), &Customer::User labelp;
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 usingreinterpre t_cast?

Your reinterpret_cas tsolution 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::Custo mer()
* * * * : TableWithId("Cu stomer", 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 SchemaBasedIdTo LabelMapper
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...@g mail.com wrote:
On 8 Ago, 19:57, mlimber <mlim...@gmail. comwrote:


On Aug 8, 9:35*am, danilo.tur...@g mail.com wrote:
* * today I encountered a problem that I'm only able to solve by using
>reinterpret_ca st(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(co nst string& name) : name(name) {
* * cout << name << endl;
}
// Class Customer
struct Customer : public Table {
* * const int Id;
* * const int Userlabel;
* * Customer();
};
Customer::Custo mer() : Table("Customer "), Id(1), Userlabel(2) {
}
// Class SchemaBasedIdTo LabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdTo LabelMapper {
public:
* * SchemaBasedIdTo LabelMapper();
};
template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdTo LabelMapper<C, idColumn,
labelColumn>::S chemaBasedIdToL abelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColum n: " << c.*labelColumn << endl;
}
int main() {
* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;}
-----------------------------------------
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(co nst string& name) : name(name) {
* * cout << name << endl;
}
// Class TableWithId (intermediate class)
struct TableWithId : public Table {
* * const int Id;
* * TableWithId(con st string& name, int id);
};
TableWithId::Ta bleWithId(const string& name, int id) : Table(name),
Id(id) {
}
// Class Customer
struct Customer : public TableWithId {
* * const int Userlabel;
* * Customer();
};
Customer::Custo mer() : TableWithId("Cu stomer", 1), Userlabel(2) {
}
// Class SchemaBasedIdTo LabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdTo LabelMapper {
public:
* * SchemaBasedIdTo LabelMapper();
};
template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdTo LabelMapper<C, idColumn,
labelColumn>::S chemaBasedIdToL abelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColum n: " << c.*labelColumn << endl;
}
int main() {
* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;}
-----------------------------------------
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::I d in reality is
&TableWithId::I d 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:
* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;
to:
* * SchemaBasedIdTo LabelMapper<Cus tomer,reinterpr et_cast<const int
Customer::*>(&C ustomer::Id), &Customer::User labelp;
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 usingreinterpre t_cast?
Your reinterpret_cas tsolution 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::Custo mer()
* * * * : TableWithId("Cu stomer", 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 SchemaBasedIdTo LabelMapper
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(co nst string& name) : name(name) {
cout << name << endl;
}
// Class TableWithId (intermediate class)
struct TableWithId : public Table {

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

TableWithId(con st string& name, int id);

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

Customer();

};
Customer::Custo mer() : TableWithId("Cu stomer", 1), Userlabel(2) {
}
// Class SchemaBasedIdTo LabelMapper
template<class C, const int C::TableWithId: :* idColumn, const int C::*
labelColumn>
class SchemaBasedIdTo LabelMapper {
public:
SchemaBasedIdTo LabelMapper();
};
template<class C, const int C::TableWithId: :* idColumn, const int C::*
labelColumn>
SchemaBasedIdTo LabelMapper<C, idColumn,
labelColumn>::S chemaBasedIdToL abelMapper() {
cout << (void *) this << endl;
C c;
cout << "IdColumn: " << c.*idColumn << endl;
cout << "LabelColum n: " << c.*labelColumn << endl;
}
int main() {
SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;

}

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


On 8 Ago, 19:57, mlimber <mlim...@gmail. comwrote:
On Aug 8, 9:35*am, danilo.tur...@g mail.com wrote:
* * today I encountered a problem that I'm only able to solve by using
reinterpret_cas t(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(co nst string& name) : name(name) {
* * cout << name << endl;
}
// Class Customer
struct Customer : public Table {
* * const int Id;
* * const int Userlabel;
* * Customer();
};
Customer::Custo mer() : Table("Customer "), Id(1), Userlabel(2) {
}
// Class SchemaBasedIdTo LabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdTo LabelMapper {
public:
* * SchemaBasedIdTo LabelMapper();
};
template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdTo LabelMapper<C, idColumn,
labelColumn>::S chemaBasedIdToL abelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColum n: " << c.*labelColumn << endl;
}
int main() {
* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;}
-----------------------------------------
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(co nst string& name) : name(name) {
* * cout << name << endl;
}
// Class TableWithId (intermediate class)
struct TableWithId : public Table {
* * const int Id;
* * TableWithId(con st string& name, int id);
};
TableWithId::Ta bleWithId(const string& name, int id) : Table(name),
Id(id) {
}
// Class Customer
struct Customer : public TableWithId {
* * const int Userlabel;
* * Customer();
};
Customer::Custo mer() : TableWithId("Cu stomer", 1), Userlabel(2) {
}
// Class SchemaBasedIdTo LabelMapper
template<class C, const int C::* idColumn, const int C::* labelColumn>
class SchemaBasedIdTo LabelMapper {
public:
* * SchemaBasedIdTo LabelMapper();
};
template<class C, const int C::* idColumn, const int C::* labelColumn>
SchemaBasedIdTo LabelMapper<C, idColumn,
labelColumn>::S chemaBasedIdToL abelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColum n: " << c.*labelColumn << endl;
}
int main() {
* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;}
-----------------------------------------
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::I d in reality is
&TableWithId::I d 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:
* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;
to:
* * SchemaBasedIdTo LabelMapper<Cus tomer,reinterpr et_cast<const int
Customer::*>(&C ustomer::Id), &Customer::User labelp;
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 usingreinterpre t_cast?
Your reinterpret_cas tsolution 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::Custo mer()
* * * * : TableWithId("Cu stomer", 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 SchemaBasedIdTo LabelMapper
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(co nst string& name) : name(name) {
* * cout << name << endl;

}

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

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

* * TableWithId(con st string& name, int id);

};

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

}

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

* * Customer();

};

Customer::Custo mer() : TableWithId("Cu stomer", 1), Userlabel(2) {

}

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

};

template<class C, const int C::TableWithId: :* idColumn, const int C::*
labelColumn>
SchemaBasedIdTo LabelMapper<C, idColumn,
labelColumn>::S chemaBasedIdToL abelMapper() {
* * cout << (void *) this << endl;
* * C c;
* * cout << "IdColumn: " << c.*idColumn << endl;
* * cout << "LabelColum n: " << c.*labelColumn << endl;

}

int main() {
* * SchemaBasedIdTo LabelMapper<Cus tomer, &Customer::I d,
&Customer::User labelp;

}

//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
4415
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 characters as possible will be matched. the regular expression module fails to perform non-greedy matches as described in the documentation: more than "as few characters as possible"
5
3737
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 their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const reference. now, casting a const return object from a function to a non-const reference to this...
3
12221
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 nonblocking mode in c++? I did something ugly like --- c/c++ mixture --- mkfifo( "testpipe", 777);
25
7588
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
4502
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
12025
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 had a static function, and the one class had a non-static function. Both of these functions did the exact same thing. The test function: public void Test(){ decimal y = 2; decimal x = 3;
11
3419
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 vtable pointer is made use of.. eg. class one {
399
12775
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 to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the following identifiers would also become valid as class, function, or variable names: Löffelstiel,...
13
18931
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 the Post: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/e81cd9d9c2200d74/48c0774eeb3bd998?hl=en&lnk=gst&q=initial+value+of+reference+to+non-const#48c0774eeb3bd998 ...
12
29851
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
8053
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8557
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8380
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6047
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5504
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2519
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 we have to send another system
0
1374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.