//
// czstring and wzstring
//
// These are "tag" typedefs for C-style strings (i.e. null-terminated character arrays)
// that allow static analysis to help find bugs.
//
// There are no additional features/semantics that we can find a way to add inside the
// type system for these types that will not either incur significant runtime costs or
// (sometimes needlessly) break existing programs when introduced.
//
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
using basic_zstring = CharT*;
template <std::ptrdiff_t Extent = dynamic_extent>
using czstring = basic_zstring<const char, Extent>;
...
template <std::ptrdiff_t Extent = dynamic_extent>
using zstring = basic_zstring<char, Extent>;
我们使用这样的东西:
#include <iostream>
#include <gsl/gsl>
int main()
{
gsl::czstring<> s = "hello, world!\n";
std::cout << s;
}
这些是指南支持库中的类型(Microsoft 提供了一个实现)。在这个实现中使用 GSL 非常简单——整个库都在头文件中,因此不需要链接。只需从存储库下载目录
include/gsl/并将其复制到您的项目所在的文件夹中。然后在编译器设置中设置头文件的路径。例如,对于 GCC 和 Clang,它看起来像这样:没找到文档(可能是我没看好),所以可以直接从头文件中找到类型(我们感兴趣
string_span.h):我们使用这样的东西: