Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement << operator for std basic_ostream and etl string_view #1040

Conversation

mike919192
Copy link
Contributor

I recently wanted to print the contents of etl::string_view to std::cout and I realized that there is not yet an overload for the << operator. I decided to experiment with adding the function overload and all seems to be working.

I put the function overload and unit test behind ETL_USING_STL macro. It will not be including in no stl configs. For the unit test I am using std::stringstream variants, but I have also tested with std::cout.

If this is acceptable I would think that the same could be done for etl::string. Let me know of any comments. Thanks

@jwellbelove
Copy link
Contributor

jwellbelove commented Mar 3, 2025

It would probably be better to create overloads of std::ostream << operator for etl::ibasic_string and etl::basic_string_view.

@mike919192
Copy link
Contributor Author

Sorry I'm not sure what you mean by the comment. std::ostream is a typedef for std::basic_ostream<char>. I implemented the template function so that it can be used with char / wchar_t / etc. That way the function can be used with whatever char type the stream supports.

The definition for the function I added:

template <typename T>
std::basic_ostream<T, std::char_traits<T> > &operator<<(std::basic_ostream<T, std::char_traits<T> > &os, 
                                                        etl::basic_string_view<T, etl::char_traits<T> > text)

@jwellbelove
Copy link
Contributor

jwellbelove commented Mar 3, 2025

Sorry, my mistake.
I've been a bit busy recently and given the pull request my full attention!

@jwellbelove
Copy link
Contributor

Add the overload for etl::ibasic_string<T> and I'll merge the pull request.

@mike919192
Copy link
Contributor Author

No problem. I will add the same overload for etl::ibasic_string later today.

@mike919192
Copy link
Contributor Author

Add the overload for etl::ibasic_string<T> and I'll merge the pull request.

It's implemented and I think I have all the tests covered. Let me know if anything is missing.

@jwellbelove jwellbelove changed the base branch from master to pull-request/#1040-Implement-stream-operator-for-std-basic_ostream-and-etl-string_view March 4, 2025 09:36
@jwellbelove jwellbelove merged commit 2b27c28 into ETLCPP:pull-request/#1040-Implement-stream-operator-for-std-basic_ostream-and-etl-string_view Mar 4, 2025
63 checks passed
@mike919192 mike919192 deleted the string_view_to_stream branch March 7, 2025 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants