dimanche 6 décembre 2015

Why can I create unique_ptr with decayed array pointer using make_unique?

Specifically, I'm wondering why this compiles:

#include <memory>
#include "make_unique.hpp"
void foo(const char *s){
   std::unique_ptr<const char*>ptr = std::make_unique<const char*>(s);
}

and this does not:

#include <memory>
#include "make_unique.hpp"
void foo(const char *s){
   std::unique_ptr<const char*>ptr(s);
}

Seems to be working when I write it like in make_unique implementation:

#include <memory>
#include "make_unique.hpp"
#include <iostream>
std::unique_ptr<const char*> foo(const char *s){
   /* return std::make_unique<const char*>(s); */
   return std::unique_ptr<const char*>(new decltype(s)(std::forward<decltype(s)>(s)));
}
int main(){
    const char * s = "bar";
    std::unique_ptr<const char*>ptr = foo(s);
    std::cout<<*ptr;

}

Aucun commentaire:

Enregistrer un commentaire