1

As the title says, the following code snippet generates C2027:

#include<regex>

using namespace std;

int main() {

    basic_regex<char8_t> r;
    return 0;
}

And the error msg was:

error C2027: use of undefined type 'std::regex_traits<char8_t>'
message : see declaration of 'std::regex_traits<char8_t>'
message : see reference to class template instantiation 'std::basic_regex<char8_t,std::regex_traits<char8_t>>' being compiled
error C2061: syntax error: identifier 'locale_type'
error C2027: use of undefined type 'std::regex_traits<char8_t>'
message : see declaration of 'std::regex_traits<char8_t>'
error C2061: syntax error: identifier 'string_type'
error C3646: 'imbue': unknown override specifier
error C2059: syntax error: '('
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
error C3646: 'getloc': unknown override specifier
error C2059: syntax error: '('
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
error C2079: 'std::basic_regex<char8_t,std::regex_traits<char8_t>>::_Traits' uses undefined class 'std::regex_traits<char8_t>'  

But basic_regex<char> works

Can someone tell me why?

char and char8_t look like the same type in terms of their usage...

I'm using VS2021

user438383
  • 5,716
  • 8
  • 28
  • 43
Suric
  • 53
  • 1
  • 10
  • 2
    `char` and `char8_t` may "look like" the same type to you, but to the compiler they're not. The same signedness and size does not guarantee that 2 integral types are the same... – fabian Jan 02 '22 at 12:52
  • 1
    @fabian So they are distinct types, But why I can‘t even instantiate basic_regex – Suric Jan 02 '22 at 13:00
  • You might want to look at the answers to this questions [Is C++20 'char8_t' the same as our old 'char'?](https://stackoverflow.com/questions/57402464/is-c20-char8-t-the-same-as-our-old-char). This explains why those are not the same. – t.niese Jan 02 '22 at 13:04
  • `But why I can‘t even instantiate basic_regex`, because the specialization of `std::regex_traits` for `char8_t` is not implemented (maybe also other specializations for `char8_t` are missing too) – t.niese Jan 02 '22 at 13:05
  • @t.niese oh thx, maybe I should use a wrapper class between regex and u8string – Suric Jan 02 '22 at 13:10
  • @Suric why? Just implement these traits. – Osyotr Jan 02 '22 at 13:27
  • 1
    @t.niese It's allowed to specialize `std::regex_traits` (as well as most other templates in `std`). But the specialization must be on a user-defined type, i.e. not on `chat8_t`. – interjay Jan 02 '22 at 13:51
  • At least your compiler told you. gcc just throws an exception at runtime. – pipe Mar 27 '22 at 22:21

0 Answers0