Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concept for string-like object

Is there a concept for a string-like object in standard library? What I mean is something like

template<typename T>
concept StringLike = requires(T x) {
   /* is same as const char*, char*, std::string etc */
}

There is a question about handmade Type trait for strings. But I cannot found anything about concepts. Why isn't it included in std?

like image 361
SherAndrei Avatar asked Apr 20 '26 19:04

SherAndrei


1 Answers

I was looking for a concept for a function, which accepts only const char*, char* and strings

template<class T>
concept StringLike = std::is_convertible_v<T, std::string_view>;

did the trick for me :)

like image 64
SherAndrei Avatar answered Apr 22 '26 07:04

SherAndrei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!