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

Correct all stream operators #35

Closed
wants to merge 1 commit into from
Closed

Correct all stream operators #35

wants to merge 1 commit into from

Conversation

glenfe
Copy link
Member

@glenfe glenfe commented Mar 11, 2017

  • Don't define these inside namespace std.
  • Don't use function template specialization. Just regular overloading is sufficient.
  • Use generic basic_ostream and basic_istream instead of ostream and istream.
  • Move the common functionality into one header and reduce duplication.
  • Remove friend declarations: Can be implemented without granting friendship.

@robertramey
Copy link
Member

robertramey commented Mar 11, 2017 via email

@glenfe
Copy link
Member Author

glenfe commented Mar 12, 2017

Robert, see the updated pull request, which also includes a test case. No linker errors like the ones you encountered. Tested with g++ 6.3.1.

@glenfe glenfe changed the title Correct stream output operators for interval Correct stream output operators Mar 12, 2017
include/io.hpp Outdated
{
int result;
is >> result;
value = result;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't you risking silent truncation here? (and the overload below)
I would expect this to trigger -Wconversion

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point; that new abstraction isn't the best idea for input. I updated the latter for parity with what he had before.

@glenfe glenfe mentioned this pull request Mar 16, 2017
@akrzemi1
Copy link
Member

The specializations are needed only for char types in order to avoid character representation. Maybe instead of specializing/overloading the interface functions just use function as_integer in the implementation, and provide overloads for as_integer that do the right conversions for display purposes:

// form Steven Watanabe's test case:

template<class T>
auto as_integer(T t) { return t; }

auto as_integer(char ch) { return (int)ch; }
auto as_integer(signed char ch) { return (int)ch; }
auto as_integer(unsigned char ch) { return (int)ch; }

@glenfe glenfe changed the title Correct stream output operators Correct all stream operators Mar 16, 2017
@glenfe
Copy link
Member Author

glenfe commented Mar 16, 2017

Andrzej, agreed (and updated).

I provided both template<class T> const T& output(const T&); and template<class T> T output(const T&&); even though that isn't required here, being a little more generic doesn't hurt.

@robertramey
Copy link
Member

fixed - thanks for your help and efforts

@robertramey robertramey closed this Apr 8, 2017
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.

None yet

4 participants