std::string::data() 不会 return 一个字符 *
std::string::data() won't return a char*
下面的程序编译失败,请问:
- GCC 6.3.0 尚未实现 C++17 功能
CharT* std::string::data()
;
- 我的代码不符合 C++17。
根据 the documentation of std::string::data()
on cppreference,自 C++17 起,此函数可以 return 一个指向 用作字符存储的底层数组的非常量指针 。
代码
#include <string>
int main() {
std::string hello("world");
char* data = hello.data();
(void) data;
}
编译
g++ --version ; g++ -std=c++17 -O2 -Wall -Werror main.cpp
输出
g++ (GCC) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
main.cpp: In function 'int main()':
main.cpp:6:28: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
char* data = hello.data();
~~~~~~~~~~^~
演示
根据the libstdc++ documentation, the feature you're looking for is implemented in libstdc++ version 7. It's listed as Give 'std::string' a non-const '.data()' member function, proposal P0272R1。
同样,the libc++ documentation 列出了与 libc++ 版本 3.9 中实现的相同的提案。
下面的程序编译失败,请问:
- GCC 6.3.0 尚未实现 C++17 功能
CharT* std::string::data()
; - 我的代码不符合 C++17。
根据 the documentation of std::string::data()
on cppreference,自 C++17 起,此函数可以 return 一个指向 用作字符存储的底层数组的非常量指针 。
代码
#include <string>
int main() {
std::string hello("world");
char* data = hello.data();
(void) data;
}
编译
g++ --version ; g++ -std=c++17 -O2 -Wall -Werror main.cpp
输出
g++ (GCC) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
main.cpp: In function 'int main()':
main.cpp:6:28: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
char* data = hello.data();
~~~~~~~~~~^~
演示
根据the libstdc++ documentation, the feature you're looking for is implemented in libstdc++ version 7. It's listed as Give 'std::string' a non-const '.data()' member function, proposal P0272R1。
同样,the libc++ documentation 列出了与 libc++ 版本 3.9 中实现的相同的提案。