2013-07-27 15 views
21

Il mio compilatore non supporta make_unique. Come scriverne uno?Come implementare la funzione make_unique in C++ 11?

template< class T, class... Args > unique_ptr<T> make_unique(Args&&... args); 
+0

possibile duplicato di [C'è un modo per scrivere make_unique() in VS2012?] (Http://stackoverflow.com/q/12547983/341970) – Ali

risposta

20

Copiato da make_unique and perfect forwarding (lo stesso è indicato in Herb Sutter's blog)

template<typename T, typename... Args> 
std::unique_ptr<T> make_unique(Args&&... args) 
{ 
    return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); 
} 

Se avete bisogno in VC2012, vedere Is there a way to write make_unique() in VS2012?


Tuttavia, se la soluzione in sasha.sochka's answer compila con il compilatore, vorrei andare con quello. Questo è più elaborato e funziona anche con gli array.

36

Versione da Stephan T. Lavavej (conosciuto anche con STL), che originariamente proposto di aggiungere questa funzione per C++ 14

#include <cstddef> 
#include <memory> 
#include <type_traits> 
#include <utility> 

namespace std { 
    template<class T> struct _Unique_if { 
     typedef unique_ptr<T> _Single_object; 
    }; 

    template<class T> struct _Unique_if<T[]> { 
     typedef unique_ptr<T[]> _Unknown_bound; 
    }; 

    template<class T, size_t N> struct _Unique_if<T[N]> { 
     typedef void _Known_bound; 
    }; 

    template<class T, class... Args> 
     typename _Unique_if<T>::_Single_object 
     make_unique(Args&&... args) { 
      return unique_ptr<T>(new T(std::forward<Args>(args)...)); 
     } 

    template<class T> 
     typename _Unique_if<T>::_Unknown_bound 
     make_unique(size_t n) { 
      typedef typename remove_extent<T>::type U; 
      return unique_ptr<T>(new U[n]()); 
     } 

    template<class T, class... Args> 
     typename _Unique_if<T>::_Known_bound 
     make_unique(Args&&...) = delete; 
} 

EDIT: codice aggiornato alla revisione N3656 standard di

+0

Come funziona la struttura '_Known_bound' quando non si usa' _Unique_if 'specifica un secondo argomento template e non c'è un valore predefinito? –

+2

@BenJackson L'intento è che non ci sia mai una corrispondenza per la versione '_Known_bound' quando' make_unique' è usato correttamente. Se qualcuno tenta di usarlo come 'make_unique ()' corrisponderà alla versione '_Known_bound' e causerà un errore. – Praetorian

+2

Qual è un buon modo per includere questa definizione nel codice C++ che potrebbe eventualmente essere compilata in un ambiente in cui make_unique è disponibile in stdC++? –

Problemi correlati