只需使用模板打印包含std::wstring 或std::wstring_view 的任何容器类型(为了演示,两个任意类型限制;根据您的需要轻松调整或删除这些限制)
我更喜欢坚持使用更普遍可读的代码(C++“概念”非常先进,没有被广泛理解)。为什么不直接使用这个简单的模板?
template <typename T>
void print(const T& matrix) {
for (auto const& str : matrix) {
for (auto const ch : str) {
std::wcout << ch;
}
std::wcout << '\n';
}
}
作为奖励,添加此static_assert 以检查类型并确保仅传入std::wstring 或std::wstring_view 字符串类型,例如(根据需要修改或删除静态断言,并根据您的需求):
static_assert(std::is_same_v<decltype(str), const std::wstring&> ||
std::is_same_v<decltype(str), const std::wstring_view&>,
"Only strings of `std::wstring` or `std::wstring_view` are "
"allowed!");
现在您有了这个更好版本的print()函数模板:
template <typename T>
void print(const T& matrix) {
for (auto const& str : matrix) {
static_assert(std::is_same_v<decltype(str), const std::wstring&> ||
std::is_same_v<decltype(str), const std::wstring_view&>,
"Only strings of `std::wstring` or `std::wstring_view` are "
"allowed!");
for (auto const ch : str) {
std::wcout << ch;
}
std::wcout << '\n';
}
}
但是,auto 的第二次使用不是必需的,也没有增加任何价值(它只是混淆事物),所以让我们删除它,并改用它:
for (wchar_t const ch : str) {
auto 的第一次用法很好,因为它是必需的,因为它可能是多种类型。
(注意:如果您确实需要在这里处理其他类型的字符,请忽略我在这里所说的并将wchar_t 改回auto。这由您决定。) em>
现在,我们有了这个最终版本的printf() 函数模板:
template <typename T>
void print(const T& matrix) {
for (auto const& str : matrix) {
static_assert(std::is_same_v<decltype(str), const std::wstring&> ||
std::is_same_v<decltype(str), const std::wstring_view&>,
"Only strings of `std::wstring` or `std::wstring_view` are "
"allowed!");
for (wchar_t const ch : str) {
std::wcout << ch;
}
std::wcout << '\n';
}
}
看来您的目标是能够使用您的自定义 print() 函数打印任何包含宽字符文本的容器类型,不是吗?
您似乎将其称为“矩阵”,其中容器中的外部元素是一个字符串,每个字符串的内部元素是一个宽字符(wchar)。
如果是这样,以下模板可以正常工作。我只是改变了这个:
void print(std::span<std::span<wchar_t>> matrix) {
到这里:
template <typename T>
void print(const T& matrix) {
...然后我补充说:
- a
static_assert 依赖于 A) std::is_same_v<>(与 std::is_same<>::value 相同)和 B) decltype() 说明符以确保仅传入 std::wstring 或 std::wstring_view 字符串类型,并且
-
main() 中的更多测试打印,包括std::vector<std::wstring> 和std::vector<std::wstring_view> 的测试打印,以及链表:std::list<std::wstring_view> 和无序集(哈希集):std::unordered_set<std::wstring> .
这是完整的代码和print() 函数模板。在线运行此代码:https://godbolt.org/z/TabW43Yjf。
#include <iostream>
#include <list> // added for demo purposes to print a linked list in main()
// #include <span> // not needed
#include <string>
#include <type_traits> // added to check types and aid with static asserts
#include <unordered_set>
#include <vector>
template <typename T>
void print(const T& matrix) {
for (auto const& str : matrix) {
static_assert(std::is_same_v<decltype(str), const std::wstring&> ||
std::is_same_v<decltype(str), const std::wstring_view&>,
"Only strings of `std::wstring` or `std::wstring_view` are "
"allowed!");
for (wchar_t const ch : str) {
std::wcout << ch;
}
std::wcout << '\n';
}
}
int main() {
std::vector<std::wstring> vec1 = {L"Cool1", L"Cool2", L"Cool3"};
std::vector<std::wstring_view> vec2 = {L"Hey1", L"Hey2", L"Hey3"};
std::list<std::wstring_view> list1 = {L"You1", L"You2", L"You3"};
std::unordered_set<std::wstring> set1 = {L"There1", L"There2", L"There3"};
print(vec1);
print(vec2);
print(list1);
print(set1);
// Compile-time error due to the std::is_same_v<> usage in the static_assert
// above!
// std::vector<std::string> vec3 = {"hey", "you"};
// print(vec3);
}
样本输出:
Cool1
Cool2
Cool3
Hey1
Hey2
Hey3
You1
You2
You3
There3
There2
There1
如果您只想打印 std::vector<std::wstring> 和 std::vector<std::wstring_view> 类型,这里有一个更有限的模板(再次,为了演示,这是两个任意类型限制;您可以根据需要轻松调整或删除这些限制):
只需在我上面的模板中替换它:
template <typename T>
void print(const T& matrix) {
这样,强制它只接受std::vector<> 容器类型(上面的const T& 更改为下面的const std::vector<T>&,就是全部):
template <typename T>
void print(const std::vector<T>& matrix) {
然后,根据需要添加static_assert 以确保向量内的类型为std::wstring 或std::wstring_view。
下面的完整代码。在此处在线运行:https://godbolt.org/z/qjhqq647M。
#include <iostream>
// #include <span> // not needed
#include <string>
#include <type_traits>
#include <vector>
template <typename T>
void print(const std::vector<T>& matrix) {
static_assert(std::is_same_v<T, std::wstring> ||
std::is_same_v<T, std::wstring_view>,
"Only vectors of `std::wstring` or `std::wstring_view` are allowed!");
for (auto const& str : matrix) {
for (wchar_t const ch : str) {
std::wcout << ch;
}
std::wcout << '\n';
}
}
int main() {
std::vector<std::wstring> vec1 = {L"Cool1", L"Cool2", L"Cool3"};
std::vector<std::wstring_view> vec2 = {L"Hey1", L"Hey2", L"Hey3"};
print(vec1);
print(vec2);
// Compile-time error due to the std::is_same_v<> usage in the static_assert
// above!
// std::vector<std::string> vec3 = {"hey", "you"};
// print(vec3);
}
为什么 span 的 span 不起作用:
std::span<T> 本质上只是一个包含指向连续内存块的指针的结构。 Cppreference.com states(强调):
类模板跨度描述了一个对象,该对象可以引用对象的连续序列,其中序列的第一个元素位于零位置。
正如我在关于跨度 (What is a "span" and when should I use one?) 的其他答案中解释的那样,它可能看起来像这样:
template <typename T>
struct span
{
T * ptr_to_array; // pointer to a contiguous C-style array of data
// (which memory is NOT allocated or deallocated
// by the span)
std::size_t length; // number of elements in the array
// Plus a bunch of constructors and convenience accessor methods here
}
但是,并非所有 C++ 容器类型都存储在连续内存中,例如链表(std::list 和 std::forward_list),因此它们不能放入跨度中。
一般来说,span 是 C++ 中的一个包装器,用于包装 C 样式的数组,在一个变量中捕获指向其连续内存块的指针,在另一个变量中捕获它们的长度。这样,您可以用两个输入参数替换函数原型,如下所示:
void do_stuff(T *ptr_to_data, std::size_t num_elements) {}
// OR (the const form)
void do_stuff(const T *ptr_to_data, std::size_t num_elements) {}
带有一个输入参数的原型如下:
void do_stuff(std::span<T> data) {}
// OR (the const form)
void do_stuff(const std::span<T> data) {}
正如@mcilloni 所说的in his comment here。
参考资料:
- 从头开始工作:
- https://godbolt.org/z/s99dnzj8z
- https://godbolt.org/z/33vzTM787
- [我的回答]What is a "span" and when should I use one?
-
https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdstring_view/ - 关于什么是
std::string_view、何时以及为何使用它以及如何使用它的优秀读物。它还涵盖了它的一些细微差别、局限性和缺点。
- https://en.cppreference.com/w/cpp/container/span
- https://en.cppreference.com/w/cpp/types/is_same
- https://en.cppreference.com/w/cpp/header/type_traits
- *****[我的答案——非常有用——我引用它是为了记住如何在编译时使用
static_assert(std::is_same_v<decltype(var), some_type>, "some msg"); 静态检查类型] Use static_assert to check types passed to macro