Std::optional als Ausgabeparameter einer FunktionC++

Programme in C++. Entwicklerforum
Guest
 Std::optional als Ausgabeparameter einer Funktion

Post by Guest »

Ich schreibe eine Funktion bool mysystem(cmd, Output), um einen Shell-Befehl ähnlich wie std::system() auszuführen, aber
  • Der Rückgabewert ist (pclose() == EXIT_SUCCESS);
  • Der Ausgabeparameter, falls vorhanden, dann mit der Ausgabe der Shell zuweisen, oder tun nichts
  • Um es einfach zu halten, müssen Benutzer keine Definitionen vornehmen Jede std::optional-Variable, der normale std::string reicht aus.
Die Frage ist: Warum kann ich optional im folgenden Code nicht in optional ändern?
Auch func(string & output="") ist das, was ich wollte, aber in C++ ungültig.

Code: Select all

#include 
#include 

using namespace std;

// bool mysystem(const string & cmd, optional output=nullopt)
bool mysystem(const string & cmd, optional output=nullopt)
{
if (output)
output.emplace("changed");
return true;
}

int main(int argc, char *argv[])
{
string cmd, out;
mysystem(cmd);      cout

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post