I would like to construct a std::basic_string_view<T>
, but the T
is the customized class.
Here is the test code:
#include <string_view>
#include <vector>
struct Token
{
Token();
Token(const Token& other)
{
lexeme = other.lexeme;
type = other.type;
}
std::string_view lexeme;
int type;
// equal operator
bool operator==(const Token& other)const {
return (this->lexeme == other.lexeme) ;
}
};
int main()
{
Token kw_class;
kw_class.lexeme = "a";
std::vector<Token> token_stream;
token_stream.push_back(kw_class);
token_stream.push_back(kw_class);
token_stream.push_back(kw_class);
std::basic_string_view<Token> token_stream_view{&token_stream[0], 3};
return 0;
}
I got some compiler error under G++:
[ 50.0%] g++.exe -Wall -fexceptions -g -c F:\code\test_crtp_twoargs\main.cpp -o obj\Debug\main.o
In file included from F:\code\test_crtp_twoargs\main.cpp:4:
F:/msys2/mingw64/include/c++/12.2.0/string_view: In instantiation of 'class std::basic_string_view<Token>':
F:\code\test_crtp_twoargs\main.cpp:98:35: required from here
F:/msys2/mingw64/include/c++/12.2.0/string_view:103:21: error: static assertion failed
103 | static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
| ^~~~~~~~~~~~~~~~~~~~
F:/msys2/mingw64/include/c++/12.2.0/string_view:103:21: note: 'std::is_trivial_v<Token>' evaluates to false
The problem happens in such assert:
template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
class basic_string_view
{
static_assert(!is_array_v<_CharT>);
static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
static_assert(is_same_v<_CharT, typename _Traits::char_type>);
public:
I really don't know where can I supply a function is_trivial_v
for the custom class Token
?
Any ideas on how to solve this issue? Thanks.