ich den folgenden Code haben:Mit Zeiger Template-Parameter
struct Port {
int reg1;
int reg2;
};
#define PORT1 ((Port *) 0x00010000); // absolutely compile-time constants
#define PORT2 ((Port *) 0x00020000);
template <Port * port>
class PortWrapper {
public:
PortWrapper() {
port->reg1 = 1;
port->reg2 = 2;
}
};
constexpr static const Port * const port1c = PORT1;
int main(int argc, char* argv[]) {
PortWrapper<PORT1> port1; //Compiler says: error: could not convert template argument '65536u' to 'Port*'
PortWrapper<port1c> port1; //Compiler says: error: 'port1c' is not a valid template argument because 'port1c' is a variable, not the address of a variable
}
Wie kann ich diese Vorlage instanziiert?
Ich kann dies tun:
Port port;
int main() {
PortWrapper<&port> port1;
}
Aber das ist nicht das, was ich brauche. Ich brauche einen Port, der auf eine vordefinierte konstante Adresse abgebildet wird.
+1: Genau was ich tun würde. (Immer ein guter Grund; P) –
Seit wann kann ein 'reinterpret_cast' ein' constexpr' sein? Ist das C++ 14? (Ich meine, sicher, das wird kompiliert, aber das Ergebnis wird * nicht * zur Kompilierzeit ausgewertet) –
@KonradRudolph Sie haben recht, es braucht einen C-Style-Cast. Geändert. Ich bin mit VS2010 festgefahren, also bin ich mit den constexpr-Regeln nicht auf Hochtouren. Und ich würde sagen, dass unabhängig davon, ob es der Definition von "constexpr" entspricht, jeder vernünftige Compiler das in der Kompilierzeit auswerten wird. – Angew