Vector initialisieren



  • Hallo C++ Community,

    unter https://godbolt.org/z/u2LE3Z wird ein Vector mit

    std::vector quadrate{1,4,9,16,25}
    

    initialisiert.

    Wenn ich den Code downloade und selber kompiliere,
    erhalte ich eine Fehlermeldung

    Compiler Explorer C++ Editor #1 Code.cpp:4:17: error: missing template arguments before ‘quadrate’```cpp
    
    
    Wenn ich den Code ändere zu:
    ```cpp
    std::vector <int> quadrate{1,4,9,16,25}
    

    funktioniert das Kompilieren.

    Warum ist das so?

    Gruß

    Piet



  • Mit welchem C++-Standard kompilierst du? Dieses Feature funktioniert seit C++17 (auch schon wieder 4 Jahre her, wie schnell die Zeit vergeht...), siehe auch den entsprechenden Switch in deinem Compiler-Explorer-Link (-std=c++17).



  • Vielen Dank für Deine Antwort.

    Mit

    
    
    #include<iostream>
    
    int main() {
        if (__cplusplus == 201703L) std::cout << "C++17\n";
        else if (__cplusplus == 201402L) std::cout << "C++14\n";
        else if (__cplusplus == 201103L) std::cout << "C++11\n";
        else if (__cplusplus == 199711L) std::cout << "C++98\n";
        else std::cout << "pre-standard C++\n";
    }
    
    
    

    erhalte ich C++14 als Antwort.

    Kann ich mit Debian Bullseye den C++17 Standard einstellen?

    Gruß

    Piet



  • Hallo,

    das kann ich ja über flags oder Code::block einstellen.

    Klappt nun.

    Vielen Dank!!!

    Gruß

    Piet


Anmelden zum Antworten