Nie wiem czy da się to zrobić w prosty sposób. (da się, użyć basic_Xstream::rdbuf
, za przykładem @_13th_Dragona)
Co w tym kodzie jest nie tak, że rzuca błędem?
Nie jestem 100% pewien tego co piszę, ale to wydaje mi się sensowne.
Jak działa operator ternarny, a konkretniej, jak jest ustalony jego wynikowy typ? To jest opisane dokładniej w § 5.16 [expr.cond]/3
:
N3797 napisał(a)
if the second and third operand have different types and either has (possibly cv-qualified) class type, or if both are glvalues of the same value category and the same type except for cv-qualification, an attempt is made to convert each of those operands to the type of the other. The process for determining whether an operand expression E1 of type T1 can be converted to match an operand expression E2 of type T2 is defined as follows:
— If E2 is an lvalue: E1 can be converted to match E2 if E1 can be implicitly converted (Clause 4) to the type “lvalue reference to T2”, subject to the constraint that in the conversion the reference must bind directly (8.5.3) to an lvalue.
— If E2 is an xvalue: E1 can be converted to match E2 if E1 can be implicitly converted to the type “rvalue reference to T2”, subject to the constraint that the reference must bind directly.
— If E2 is a prvalue or if neither of the conversions above can be done and at least one of the operands has (possibly cv-qualified) class type:
— if E1 and E2 have class type, and the underlying class types are the same or one is a base class of the other: E1 can be converted to match E2 if the class of T2 is the same type as, or a base class of, the class of T1, and the cv-qualification of T2 is the same cv-qualification as, or a greater cv-qualification than, the cv-qualification of T1. If the conversion is applied, E1 is changed to a prvalue of type T2 by copy-initializing a temporary of type T2 from E1 and using that temporary as the converted operand.
— Otherwise (i.e., if E1 or E2 has a nonclass type, or if they both have class types but the underlying classes are not either the same or one a base class of the other): E1 can be converted to match E2 if E1 can be implicitly converted to the type that expression E2 would have if E2 were converted to a prvalue (or the type it has, if E2 is a prvalue).
ostream(argv[2])
to prvalue, a cin
to lvalue. Kompilator próbuje przekonwertować jedno w drugie, ale nie może, bo basic_ostream
jest niekopiowalny, a konstruktor przenoszący jest protected.
Możesz to obejść w ohydny sposób z leakami: http://melpon.org/wandbox/permlink/Wkezq9jg7layMydV
Kopiuj
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
ostream& o = argc > 2 ? *new ofstream(argv[2], ios::out | ios::binary ) : cout;
istream& i = argc > 1 ? *new ifstream(argv[1], ios::in | ios::binary ) : cin;
o << i.rdbuf();
return 0;
}
co potem możesz upięknić¹ do: http://melpon.org/wandbox/permlink/199NAbuuc42g4IAI
Kopiuj
#include <iostream>
#include <fstream>
#include <memory>
using namespace std;
int main(int argc, char* argv[])
{
auto o = argc > 2 ? make_shared<ofstream>(argv[2], ios::out | ios::binary ) : shared_ptr<ostream>(&cout,[](void*){});
auto i = argc > 1 ? make_shared<ifstream>(argv[1], ios::in | ios::binary ) : shared_ptr<istream>(&cin,[](void*){});
*o << i->rdbuf();
return 0;
}
Swoją drogą ostatnio robiłem coś podobnego w D i tam to się odbyło bez problemów:
Kopiuj
auto inFile = args.length > 2 ? File(args[2], a == Action.Decode ? "r" : "rb") : stdin;
auto outFile = args.length > 3 ? File(args[3], a == Action.Decode ? "wb" : "w") : stdout;
¹ dla pewnych definicji piękna.
rdbuf
, więc nie byłem pewien, czy Twoja odpowiedź jest w 100% na temat :P Codziennie można się czegoś nauczyć.