c++ std::optional: Bug in STL Implementation?



  • Hi zuammen,

    ich habe mit std::optional<bool> ein Problem. Folgenden Code

    #include <optional>
    
    std::optional<bool> test_optional()
    {
       return true;
    }
    
    int main()
    {
       auto const val = test_optional();
    }
    

    quittiert mein Compiler mit der Fehlermeldung

    [bcc32c Hinweis] optional(249): in instantiation of member function 'std::_Optional_base<bool>::_Optional_base' requested here
    [bcc32c Hinweis] optional(37): candidate constructor
    [bcc32c Hinweis] optional(45): candidate constructor [with _Types = <bool>]
    [bcc32c Hinweis] optional(51): candidate constructor
    [bcc32c Hinweis] optional(31): candidate is the implicit copy constructor
    [bcc32c Hinweis] optional(31): candidate is the implicit move constructor
    

    Sieht so aus, als könnte er für std::optional<bool> nicht den richtigen Kopierkonstruktor finden.
    Die Signaturen der entsprechenden Kopierkonstruktoren sehen so aus:

    (Zeile 37)   _Optional_destroy(bool _Exist_val = false)
    (Zeile 45)   template<class... _Types>
                 _Optional_destroy(_Types&&... _Args)
    (Zeile 51)   _Optional_destroy(const _Ty& _Value)
    (Zeile 31)   ist der Beginn der Klassendefinition template<class _Ty, bool = false> class _Optional_destroy
    

    Zeile 31
    Fällt jemandem was dazu ein?

    PS:
    Der online Compiler "C++ shell" übersetzt das anstandslos.



  • Wird von Visual Studio 2022 auch ohne Beanstandung kompiliert.
    Hast Du den Compiler angewiesen, mindestens C++17 konform zu kompilieren?



  • @DocShoe sagte in c++ std::optional: Bug in STL Implementation?:

    [bcc32c Hinweis] optional(249): in instantiation of member function 'std::_Optional_base<bool>::_Optional_base' requested here
    [bcc32c Hinweis] optional(37): candidate constructor
    [bcc32c Hinweis] optional(45): candidate constructor [with _Types = <bool>]
    [bcc32c Hinweis] optional(51): candidate constructor
    [bcc32c Hinweis] optional(31): candidate is the implicit copy constructor
    [bcc32c Hinweis] optional(31): candidate is the implicit move constructor
    

    [...]
    Zeile 31
    Fällt jemandem was dazu ein?

    Leider nein, ich finde das auch merkwürdig. Ich wollte eigentlich nur darauf hinweisen, dass die eigentliche Fehlermeldung fehlt. Das da oben ist irgendwie nur die Kontextinformation wo irgendwas passiert ist. Vielleicht gibt die Fehlermeldung ja mehr Aufschluss, falls sie denn anders lauten sollte als "Kopierkonstruktor nicht gefunden" (sieht aber schon sehr danach aus, mit den "candidate"-Meldungen).


  • Mod

    Kann man mit Godbolt auch nicht reproduzieren. Mein einziger Tipp waere val mittels direct-initialization zu initialisieren, weil evt. irgendein dummer Fehler wegen explicit auftritt. D.h. val(test_optional())



  • Ist ´ne Mehrdeutigkeit für bool, da kommen zwei Kopierkonstruktoren in Betracht. Ich bau das jetzt von std::optional zu boost::optional zurück, damit gab's keine Probleme. Hab ein Ticket bei Embarcadero aufgemacht.

    Das hier geht übrigens auch nicht, aus den gleichen Gründen:

    #include <optional>
    
    int main()
    {
       std::optional<bool> const v1;
       std::optional<bool> const v2 = v1;
    }
    

Anmelden zum Antworten