2014-12-05 12 views
13

Desidero utilizzare una variabile std::atomic_int. Nel mio codice, ho:Errore "Uso della funzione eliminata" con std :: atomic_int

#include <atomic> 

std::atomic_int stop = 0; 

int main() 
{ 
    // Do something 
} 

E questo mi dà un errore di compilazione:

use of deleted function 'std::__atomic_base<_IntTp>::__atomic_base(const std::__atomic_base<_IntTp>&) [with _ITp = int]' 
std::atomic_int stop = 0; 
         ^

Qualche idea su cosa sta succedendo?

+0

sicuramente aumentare/thread.hpp è irrilevante in questa sede? Il problema persiste quando si rimuove l'intestazione? –

+0

Sì, non ha nulla a che fare con boost in realtà ... non so perché l'ho incluso! Il problema rimane anche dopo aver rimosso l'intestazione. – Karnivaurus

+0

[Repro in GCC 4.9] (http://coliru.stacked-crooked.com/a/990a12fc81ea21de) –

risposta

15

Il codice sta tentando di costruire una temporanea std::atomic_int sul RHS, quindi utilizzare il costruttore std::atomic_int copia (che è delete d) per inizializzare stop, in questo modo:

std::atomic_int stop = std::atomic_int(0); 

Questo perché copia-inizializzazione , come stai suonando qui, non è del tutto equivalente ad altri tipi di inizializzazione.

[C++11: 8.5/16]: The semantics of initializers are as follows [..]

If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).

(questo permette per l'opzione 3, al termine di questa risposta)

[..]

If the destination type is a (possibly cv-qualified) class type:

  • If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, constructors are considered. The applicable constructors are enumerated (13.3.1.3), and the best one is chosen through overload resolution (13.3). The constructor so selected is called to initialize the object, with the initializer expression or expression-list as its argument(s). If no constructor applies, or the overload resolution is ambiguous, the initialization is ill-formed.

(questo quasi descrive il codice, ma non del tutto, la chiave qui è che, forse in contrasto con l'intuizione, std::atomic_int s' i costruttori non sono considerati a tutti nel tuo caso)

  • Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one is chosen through overload resolution (13.3). If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the cv-unqualified version of the destination type. The temporary is a prvalue. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized; see 12.2, 12.8.

(questo è lo scenario,! modo, anche se la copia può essere tralasciata, ancora deve essere possibile)

  • [..]

Ecco la correzione, in ogni caso; utilizzare diretta-inizializzazione o lista-inizializzazione:

std::atomic_int stop(0);  // option 1 
std::atomic_int stop{0};  // option 2 
std::atomic_int stop = {0}; // option 3 
+0

Sì, funziona grazie. Quindi sembra che un costruttore predefinito senza argomenti non esiste, e devi fornire un valore esplicito come lo hai tu. Grazie! – Karnivaurus

+0

@Karnivaurus: non ha nulla a che fare con alcun costruttore predefinito. Stai fornendo argomenti. –

+0

Non è questo il caso per lo più di qualsiasi tipo? 'T a = x;' crea un 'T' temporaneo costruito da' x', quindi inizializza 'a' da quel temporaneo. Copia elision può eliminare il temporaneo, ma il costruttore della copia deve essere comunque disponibile. – hvd

Problemi correlati