Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/unique ptr/make unique"

From cppreference.com
< cpp‎ | memory‎ | unique ptr
(fix example)
m (Example: std::endl => '\n', indentation)
Line 49: Line 49:
 
struct Vec3
 
struct Vec3
 
{
 
{
  int x;
+
    int x;
  int y;
+
    int y;
  int z;
+
    int z;
  
  Vec3() :x(0), y(0), z(0)
+
    Vec3() :x(0), y(0), z(0)
  {
+
    {
 +
    }
  
  }
+
    Vec3(int x, int y, int z) :x(x), y(y), z(z)
 +
    {
 +
    }
  
  Vec3(int x, int y, int z) :x(x), y(y), z(z)
+
    friend std::ostream& operator<<(std::ostream& os, Vec3& v);
  {
+
 
+
  }
+
 
+
  friend std::ostream& operator<<(std::ostream& os, Vec3& v);
+
 
};
 
};
  
 
std::ostream& operator<<(std::ostream& os, Vec3& v)
 
std::ostream& operator<<(std::ostream& os, Vec3& v)
 
{
 
{
  return os << "x:" << v.x << " y:" << v.y << " z:" << v.z << std::endl;
+
    return os << "x:" << v.x << " y:" << v.y << " z:" << v.z << '\n';
 
}
 
}
  
 
auto main() -> int
 
auto main() -> int
 
{  
 
{  
  // Use the default constructor.
+
    // Use the default constructor.
 
+
    std::unique_ptr<Vec3> v1 = std::make_unique<Vec3>();
  std::unique_ptr<Vec3> v1 = std::make_unique<Vec3>();
+
 
+
  // Use the constructor that matches these arguments
+
 
+
  std::unique_ptr<Vec3> v2 = std::make_unique<Vec3>(0, 1, 2);
+
  
  // Create a unique_ptr to an array of 5 elements
+
    // Use the constructor that matches these arguments
 +
    std::unique_ptr<Vec3> v2 = std::make_unique<Vec3>(0, 1, 2);
  
  std::unique_ptr<Vec3[]> v3 = std::make_unique<Vec3[]>(5);
+
    // Create a unique_ptr to an array of 5 elements
 +
    std::unique_ptr<Vec3[]> v3 = std::make_unique<Vec3[]>(5);
  
  std::cout << "make_unique<Vec3>():" << std::endl;
+
    std::cout << "make_unique<Vec3>():" << '\n'
  std::cout << (*v1) << std::endl;
+
    std::cout << (*v1) << '\n';
  
  std::cout << "make_unique<Vec3>(0,1,2):" << std::endl;
+
    std::cout << "make_unique<Vec3>(0,1,2):" << '\n';
  std::cout << (*v2) << std::endl;
+
    std::cout << (*v2) << '\n';
  
  std::cout << "make_unique<Vec3[]>(5):" << std::endl;
+
    std::cout << "make_unique<Vec3[]>(5):" << '\n';
  for (int i = 0; i < 5; i++)
+
    for (int i = 0; i < 5; i++) {
  {
+
std::cout << v3[i] << '\n';
std::cout << (v3[i]) << std::endl;
+
    }
  }
+
  
  return 0;
+
    return 0;
 
}
 
}
 
  | output=
 
  | output=

Revision as of 04:31, 21 September 2014

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
 
Defined in header <memory>
template< class T, class... Args >
unique_ptr<T> make_unique( Args&&... args );
(1) (since C++14)
(only for non-array types)
template< class T >
unique_ptr<T> make_unique( std::size_t size );
(2) (since C++14)
(only for array types with unknown bound)
template< class T, class... Args >
/* unspecified */ make_unique( Args&&... args ) = delete;
(3) (since C++14)
(only for array types with known bound)

Constructs an object of type T and wraps it in a std::unique_ptr.

1) Constructs a non-array type T. The arguments args are passed to the constructor of T. The function does not participate in the overload resolution if T is an array type. The function is equivalent to:
unique_ptr<T>(new T(std::forward<Args>(args)...))
2) Constructs an array of unknown bound T. The function does not participate in the overload resolution unless T is an array of unknown bound. The function is equivalent to:
unique_ptr<T>(new typename std::remove_extent<T>::type[size]())
3) Construction of arrays of known bound is disallowed.

Contents

Parameters

args - list of arguments with which an instance of T will be constructed.
size - the size of the array to construct

Return value

std::unique_ptr of an instance of type T.

Exceptions

Any exception thrown by the contructor of T. If an exception is thrown, this function has no effect.

Example

#include <iostream>
#include <memory>
 
struct Vec3
{
    int x;
    int y;
    int z;
 
    Vec3() :x(0), y(0), z(0)
    {
    }
 
    Vec3(int x, int y, int z) :x(x), y(y), z(z)
    {
    }
 
    friend std::ostream& operator<<(std::ostream& os, Vec3& v);
};
 
std::ostream& operator<<(std::ostream& os, Vec3& v)
{
    return os << "x:" << v.x << " y:" << v.y << " z:" << v.z << '\n';
}
 
auto main() -> int
{ 
    // Use the default constructor.
    std::unique_ptr<Vec3> v1 = std::make_unique<Vec3>();
 
    // Use the constructor that matches these arguments
    std::unique_ptr<Vec3> v2 = std::make_unique<Vec3>(0, 1, 2);
 
    // Create a unique_ptr to an array of 5 elements
    std::unique_ptr<Vec3[]> v3 = std::make_unique<Vec3[]>(5);
 
    std::cout << "make_unique<Vec3>():" << '\n'
    std::cout << (*v1) << '\n';
 
    std::cout << "make_unique<Vec3>(0,1,2):" << '\n';
    std::cout << (*v2) << '\n';
 
    std::cout << "make_unique<Vec3[]>(5):" << '\n';
    for (int i = 0; i < 5; i++) {
	std::cout << v3[i] << '\n';
    }
 
    return 0;
}

Output:

make_unique<Vec3>():
x:0 y:0 z:0
 
make_unique<Vec3>(0,1,2):
x:0 y:1 z:2
 
make_unique<Vec3[]>(5):
x:0 y:0 z:0
 
x:0 y:0 z:0
 
x:0 y:0 z:0
 
x:0 y:0 z:0
 
x:0 y:0 z:0

See also

constructs a new unique_ptr
(public member function) [edit]