Ich bastelte, um das Beispiel auf Seite 91 von Effektivem Modern C++ zu bestätigen, und ich stieß auf etwas, das ein seltsames Problem scheint. Dieser CodeC++ noexcept Deklaration ändert Vorlage Abzug
template<typename C>
void doStuff(C& a, C& b) noexcept(noexcept(doStuff(a.front(), b.front()))) {
std::cout << "container version" << std::endl;
}
template<>
void doStuff<int>(int& x, int& y) noexcept {
std::cout << "int version" << std::endl;
}
int main() {
vector<int> v1 = {1, 2, 3};
vector<int> v2 = {4, 5, 6};
int x = 5;
int y = 6;
doStuff(x, y);
doStuff(v1, v2);
}
gibt mir einen Fehler wie
error: request for member ‘front’ in ‘a’, which is of non-class type ‘int’ void doStuff(C& a, C& b) noexcept(noexcept(doStuff(a.front(), b.front()))) {
So scheint es, wie die Top-Version von doStuff genannt wird, obwohl a.front() und b.front() sollte sein Verweise auf Ints zurückgeben. Wenn ich alle noexcept-Deklarationen aus dem Code entferne, erhalte ich die erwartete Ausgabe.
Dies ist mit gcc 5.4.
Was mache ich falsch?
Dank
Denken Sie daran, dass Vorlagen ein compile.time Feature sind. Wenn die erste Version der 'doStuff'-Funktion definiert ist, weiß der Compiler nichts über die Spezialisierung für' int'. –
Es gibt auch das Problem, dass 'doStuff' nicht deklariert wurde, wenn es zum ersten Mal in der noexcept-Spezifikation verwendet wird. – aschepler