固定大小字符串类的最佳实践

ruh*_*ner 11 c++ size fixed stdstring string-length

我想有一个固定大小的字符串类.理想情况下,接口将匹配std::string新接口永远不会分配新内存的一个区别.对于应该避免分配新内存的应用程序情况,它应该是一个方便的类.大小可以是静态的(在编译时已知).

我认为有两种方法.第一个是围绕char数组实现一个类,然后实现或多或少的所有函数std::string.我还必须实现一些运算符来创建std::string具有给定固定大小字符串的s等.

第二种方法,我甚至不确定是否可能,将继承std::string and覆盖可能改变字符串大小的所有函数.我查看了basic_stringVisual Studio中的标题,它似乎不是虚拟的,所以我想这不是要走的路.

您认为实施此类课程的最佳方法是什么?

Bar*_*rry 12

第一个是围绕char数组实现一个类,然后实现或多或少的所有函数std::string.

这绝对是要走的路.它易于编写,易于使用且难以滥用.

template <size_t N>
class fixed_string {
    char array[N+1];
    size_t size;

public:
    fixed_string() : size(0) { array[0] = '\0'; }

    // all the special members can be defaulted
    fixed_string(fixed_string const&) = default;
    fixed_string(fixed_string&&) = default;
    fixed_string& operator=(fixed_string const&) = default;
    fixed_string& operator=(fixed_string&&) = default;
    ~fixed_string() = default;

    // ...
};
Run Code Online (Sandbox Code Playgroud)

所有的访问者(data,c_str,begin,end,at,operator[])是单行.所有搜索算法都很简单.

唯一真正的设计问题是你希望突变在失败时做什么.那是:

fixed_string<5> foo("abcde");
foo += 'f'; // assert? throw? range-check internally and ignore?
            // just not even add this and instead write a 
            // try_append() that returns optional<fixed_string&>?
Run Code Online (Sandbox Code Playgroud)

设计选择有优点和缺点,但无论你选择哪一个,每个功能的实现也将非常简洁.


第二种方法,我甚至不确定是否可能,将继承std::string并覆盖可能改变字符串大小的所有函数.我查看了basic_stringVisual Studio中的标题,它似乎不是虚拟的,所以我想这不是要走的路.

无论在什么std::stringvirtual无关的这是否是一个好主意的问题.你肯定想从以下开始:

template <size_t N>
class fixed_string : private std::string { ... }
//                  ^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

因为你的类型绝对不符合is-a的关系std::string.它不是一个std::string,它只是用它来实现.私有继承会使这段代码格式不正确:

std::string* p = new fixed_string<5>();
Run Code Online (Sandbox Code Playgroud)

所以你不必担心缺乏virtual.

也就是说,继承这种string做法将会带来更复杂,效率更低的实施,而不仅仅是直接路线,还有更多潜在的陷阱.实现这样的事情可能是可能的,但我看不出它是一个好主意.