개요
- 범위의 요소를 포함하는 메모리 블록에 대한 포인터를 반환
예제
- 코드
#include <cstring>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
using namespace std;
template <typename T> constexpr string_view type_name() {
const string s = __PRETTY_FUNCTION__;
const int prefixSize = s.find("[with T = ") + strlen("[with T = ");
return string_view(s.data() + prefixSize, s.find(';') - prefixSize);
}
int main() {
int i[]{1, 2, 3};
cout << type_name<decltype(i)>() << endl;
cout << type_name<decltype(data(i))>() << endl;
cout << "------ 1" << endl;
string s{"abc"};
cout << type_name<decltype(s)>() << endl;
cout << type_name<decltype(data(s))>() << endl;
cout << "------ 2" << endl;
vector<int> v{1, 2, 3};
cout << type_name<decltype(v)>() << endl;
cout << type_name<decltype(data(v))>() << endl;
return 0;
}
- 실행 결과
int [3]
int*
------ 1
std::__cxx11::basic_string<char>
char*
------ 2
std::vector<int>
int*