Hi,
ich versuche gerade eine C++11 Wrapper-Classe für eine C-Library (nicht
von mir) zu schreiben. Die C-Lib bietet float-Funktionen, und die
Klasse ist:
1 | class Float
|
2 | {
|
3 | public:
|
4 | ~Float();
|
5 | Float();
|
6 | Float (long);
|
7 | explicit Float (double);
|
8 |
|
9 | Float (const Float&);
|
10 | const Float& operator= (const Float&);
|
11 | Float (Float&&) = default;
|
12 | Float& operator= (Float&&) = default;
|
13 | };
|
Die C-Lib stellt Initialisierungen aus long und double zur Verfügung,
und offenbar will man nicht double in integer umwandeln (oder umgekehrt)
bevor ein Float Objekt konstruiert wird.
1 | Float func ()
|
2 | {
|
3 | Float f;
|
4 | double x = 3.1e10;
|
5 | f = x;
|
6 | return f;
|
7 | }
|
Gibt dann folgende Warnung für "f = x;":
1 | warning: overflow in implicit constant conversion [-Woverflow]
|
und es wird Konstruktor Float(long) verwendet (seh ich am erzeugten
Code).
Wie schafft man es, dass Float(double) zur Construction aus double
verwendet wird?
Noch unverständlicher sind die Fehler, wenn es 'nen Constructor
gibt: Fehler für "f = x;"
1 | error: converting to 'Float' from initializer list would use explicit constructor 'Float::Float(double)'
|
Jetzt wird der Konstruktor verwendet, der verwendet werden soll, aber
das ist ein Fehler??? Die genennten Konstruktoren sind alle;
initializer_list wird noch nichtmal included.
Und es gibt nen Fehler
1 | error: initializing argument 1 of 'Float& Float::operator=(Float&&)'
|
Bei einer Zuweisung kann das Float-Objekt einfach kopieren werden wenn
die rechte Seite nicht mehr gebraucht wird.
Gleicher Fehler mit "Float& Float::operator= (const Float&&);"