0

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.

ollydbg23
  • 1,124
  • 1
  • 12
  • 38
  • `Token` needs to be a [trivial type](https://en.cppreference.com/w/cpp/types/is_trivial) – Alan Birtles Feb 02 '23 at 10:51
  • Your type does not meet the requirement for _char-like_ types: http://eel.is/c++draft/strings.general#1. – Daniel Langr Feb 02 '23 at 10:53
  • 1
    Perhaps you want to use [std::span](https://en.cppreference.com/w/cpp/container/span) instead. – BoP Feb 02 '23 at 11:06
  • Thanks for your comments. It is sad that my custom Token class can't be used, in-fact, I would like to change a parser which works on a `std::string_view`, but I would like to extent this parser, and use my own Token for the parser. I mean that I would like to create a custom Token Stream View like `std::basic_string_view`. With this change, I think the change I need is minimal. – ollydbg23 Feb 02 '23 at 12:59
  • Hi, @BoP I have consider `std::span`, one reason is that this class need `C++20` while the `std::basic_string_view` need `C++17`, you can see this post for a reference: https://stackoverflow.com/questions/67105660/difference-between-stdbasic-string-viewt-and-stdspant Another reason is that the old code of the parser use std::string_view as the input, so if I use `std::basic_string_view`, the code change may be minimal, and backward compatible. – ollydbg23 Feb 02 '23 at 13:04
  • Can I change the Token class to make it trival? – ollydbg23 Feb 02 '23 at 13:37
  • you can't `std::string_view` isn't trivial itself (until c++23), trivial objects can't have non-trivial members – Alan Birtles Feb 02 '23 at 19:50
  • Hi, @AlanBirtles thanks for the info. I'm considering using the BoP's suggestion to use the [std::span](https://en.cppreference.com/w/cpp/container/span), but it is strange that it has no function to remove the first element from the `std::span` object, just like: [std::basic_string_view::remove_prefix](https://en.cppreference.com/w/cpp/string/basic_string_view/remove_prefix) – ollydbg23 Feb 03 '23 at 01:30
  • You can use the subspan method to do that – Alan Birtles Feb 03 '23 at 07:24
  • Yes, thanks, I am considering std::span, see this question here https://stackoverflow.com/questions/75331349/what-is-the-way-to-remove-the-first-element-from-a-stdspant – ollydbg23 Feb 03 '23 at 14:50

0 Answers0