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
Conversation
glenfe
commented
Mar 11, 2017
•
edited
edited
- 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.
|
Right. But when I do this
// stream operators
template<
class CharT,
class Traits,
class T,
T Min,
T Max,
class P, // promotion polic
class E // exception policy
inline std::basic_ostream<CharT, Traits> & operator<<(
std::basic_ostream<CharT, Traits> & os,
const safe_base<T, Min, Max, P, E> & t
){
return os << (
(std::is_same<T, signed char>::value
|| std::is_same<T, unsigned char>::value
) ?
static_cast<int>(t.m_t)
:
t.m_t
);
}
and build a test which uses os << safe<int>
I get an link error:
Undefined symbols for architecture x86_64:
"std::__1::basic_ostream<char, std::__1::char_traits<char> >&
boost::numeric::operator<<<int, 0, 131070, boost::numeric::native,
boost::numeric::throw_exception>(std::__1::basic_ostream<char,
std::__1::char_traits<char> >&, boost::numeric::safe_base<int, 0,
131070, boost::numeric::native, boost::numeric::throw_exception>
const&)", referenced from:
bool test_add<unsigned short, unsigned short>(unsigned short,
unsigned short, char const*, char const*, char) in test_add_native.o
"std::__1::basic_ostream<char, std::__1::char_traits<char> >&
boost::numeric::operator<<<int, 0, 510, boost::numeric::native,
boost::numeric::throw_exception>(std::__1::basic_ostream<char,
std::__1::char_traits<char> >&, boost::numeric::safe_base<int, 0, 510,
boost::numeric::native, boost::numeric::throw_exception> const&)",
referenced from:
bool test_add<unsigned char, unsigned char>(unsigned char,
unsigned char, char const*, char const*, char) in test_add_native.o
....
Since you're up on this stuff, maybe you know the missing magic here.
…On 3/11/17 11:59 AM, Glen Fernandes wrote:
------------------------------------------------------------------------
You can view, comment on, or merge this pull request online at:
#35
Commit Summary
* Correct stream output operators for interval
File Changes
* *M* include/interval.hpp
<https://github.com/robertramey/safe_numerics/pull/35/files#diff-0>
(27)
Patch Links:
* https://github.com/robertramey/safe_numerics/pull/35.patch
* https://github.com/robertramey/safe_numerics/pull/35.diff
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#35>, or mute the
thread
<https://github.com/notifications/unsubscribe-auth/AB4R3EIyAFocVk-31dxA7aYPNIYtFfFCks5rkv0RgaJpZM4MaUO_>.
--
Robert Ramey
www.rrsd.com
(805)569-3793
|
|
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. |
include/io.hpp
Outdated
| { | ||
| int result; | ||
| is >> result; | ||
| value = result; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
The specializations are needed only for // 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; }
|
|
Andrzej, agreed (and updated). I provided both |
|
fixed - thanks for your help and efforts |