简体   繁体   English

区别:std::make_unique<char> (size_t 大小) 和 std::make_unique<char[]> (size_t 大小)?</char[]></char>

[英]Difference between: std::make_unique<char>(size_t size) and std::make_unique<char[]>(size_t size)?

I was implementing the circular array data structure whose code looks like this:我正在实现循环数组数据结构,其代码如下所示:

struct CircularArrayException : public std::exception {
    std::string msg;

    CircularArrayException(const std::string arg_msg) 
    : msg{"CircularArrayException: " + arg_msg} {}

    const char * what () const throw () {
        return msg.c_str();
    }
};

template <typename T>
class CircularArray {
public:
    const size_t array_size;
    std::unique_ptr<T> uptr_arr;

    size_t occupied_size = 0;
    int front_idx = -1;
    int back_idx = -1;
 
    CircularArray(const CircularArray& ca) = delete;

    CircularArray& operator=(const CircularArray& ca) = delete;

    CircularArray(
        const size_t arg_array_size
    ):  array_size{arg_array_size} {
        uptr_arr = std::make_unique<T>(array_size);
    };
};

After the implementation I tested the implementation with CircularArray<char> and it works fine.在实现之后,我用CircularArray<char>测试了实现,它工作正常。 But, then I realized that we use std::make_unique<char[]>(num_elements) to declare a unique_ptr to an array as opposed to std::make_unique<char>(num_elements) .但是,后来我意识到我们使用std::make_unique<char[]>(num_elements)将 unique_ptr 声明到数组而不是std::make_unique<char>(num_elements) But, even then the code seems to work fine.但是,即使那样,代码似乎也能正常工作。 I looked the documentation of std::make_uniquehere and couldn't understand the explanation of the (2)nd signature.我在这里查看了std::make_unique的文档,无法理解 (2)nd 签名的解释。 Can anyone help me out to understand the difference and why my code works?任何人都可以帮助我了解差异以及我的代码为何有效吗?

Here is the what is written on cppreference for the (2) signature:这是 (2) 签名的 cppreference 上写的内容:

template< class T >
unique_ptr<T> make_unique( std::size_t size );

(2) (since C++14) (only for array types with unknown bound) (2)(C++14 起)(仅适用于边界未知的数组类型)

Constructs an array of unknown bound T. This overload participates in overload resolution only if T is an array of unknown bound.构造一个未知边界 T 的数组。仅当 T 是一个未知边界数组时,此重载才参与重载决议。 The function is equivalent to: unique_ptr<T>(new typename std::remove_extent<T>::type[size]()) function 等价于: unique_ptr<T>(new typename std::remove_extent<T>::type[size]())

Here is the goldbolt link: https://godbolt.org/z/K9h3qTeTW这是金螺栓链接: https://godbolt.org/z/K9h3qTeTW

std::make_unique<char>(65); creates a pointer to a single character initialised with the value 65 ( 'A' ).创建指向使用值65 ( 'A' ) 初始化的单个字符的指针。 std::make_unique<char[]>(65) creates an array with 65 elements. std::make_unique<char[]>(65)创建一个包含 65 个元素的数组。

If you run this code:如果您运行此代码:

#include <memory>
#include <iostream>

int main()
{
    auto a = std::make_unique<char>(65);
    std::cout << *a << "\n";
    auto b = std::make_unique<char[]>(65);
    std::cout << (int)b[0] << "\n";
}

It'll print A for the first one and an undefined value for the second one (possibly 0) as the array elements are uninitialised.由于数组元素未初始化,它将为第一个打印A ,为第二个打印未定义的值(可能为 0)。

Your code "works" by chance, using any more than 1 element of your "array" will cause undefined behaviour.您的代码“工作”是偶然的,使用“数组”中超过 1 个元素将导致未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM