模板化 c 字符串包装器中的大小扣除
size deduction in a templated c-string wrapper
为什么这不起作用:
#include <cstring>
template<size_t sz>
struct wstr {
wchar_t _str[sz];
wstr(const wchar_t source[sz]) {
wcscpy_s(_str, source);
}
};
int main(int argc, char** argv) {
wstr ws = L"Hello"; //needs template argument
return 0;
}
L"Hello"
已知它是 const wchar_t[6]
.
要使模板推导适用于数组,您需要将参数作为对数组的引用。
wstr(const wchar_t (&source)[sz]) { ... }
为什么这不起作用:
#include <cstring>
template<size_t sz>
struct wstr {
wchar_t _str[sz];
wstr(const wchar_t source[sz]) {
wcscpy_s(_str, source);
}
};
int main(int argc, char** argv) {
wstr ws = L"Hello"; //needs template argument
return 0;
}
L"Hello"
已知它是 const wchar_t[6]
.
要使模板推导适用于数组,您需要将参数作为对数组的引用。
wstr(const wchar_t (&source)[sz]) { ... }