c++ - 用户定义的字符串文字,字符串是否终止?

标签 c++ user-defined-literals

对于用户定义的字符串文字,如果我使用以下形式的定义,给定的字符串是否保证以 null 终止?我知道第二个参数给出的大小没有任何终止,如果有的话。

void operator"" _x( const char* n, size_t s)
{
    std::cout << "String: " << s << " Len: " << s << std::endl;
}

如果我使用这个版本的定义,我看不到空终止符!

template <class T, T... Chrs>
void operator""_s() 
{
    std::cout << __PRETTY_FUNCTION__ << std::endl;
}

最佳答案

user defined string literal, is string null terminated?

void operator"" _x( const char* n, size_t s)

是的。字符串文字以 null 结尾,n 指向此类字符串文字。


If I use this version of definition I see no null termination character!

template <class T, T... Chrs>
void operator""_s()

标准不允许字符串文字模板。有文件 N3599建议将其添加到标准中,它是为 C++14 而设计的,但有 no consensus而且它还没有成为标准的一部分。 GCC 和 Clang 至少似乎已经将其实现为语言扩展。

事实上,文字运算符模板不接收空字符作为其参数之一。

Proposal N3599:

the remaining arguments are the code units in the string literal (excluding its terminating null character).

关于c++ - 用户定义的字符串文字,字符串是否终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44779780/

相关文章:

c++ - 用户定义的文字如何与数字分隔符一起使用?

c++ - 文字运算符的模板参数列表

c++ - VS2015 中的计时文字

.net - CreateEnhMetaFile 和 PlayEnhMetaFile 的 .NET 等价物是什么?

c++ - 将任意 lambda 表达式传递给函数?

c++ - 使用 unique_ptr 的常量字符指针

c++ - 指定模板参数

c++ - 如何解决字段 'classname'有不完整的类型错误

c++ - 字符串与十六进制值的用户定义文字

c++ - C++ 11用户定义的文字比普通类型转换慢吗?