模板化c字符串包装器中的大小扣除

为什么这不起作用:

#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]) { ... }

  • @pink tell your compiler to use C++17. I am staring at your code, modified to use a reference there, and compiling.

以上是模板化c字符串包装器中的大小扣除的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>