Can't use overloaded operator<< to print object's value - c++

I wrote tuple implementation, which seems to work:
template<typename T, typename... U>
struct tuple{
T first;
tuple<U...> second;
tuple()=default;
tuple(T t, U... u):first(t), second(u...){}
std::ostream& print(std::ostream& stream){
stream<<first<<", ";
return second.print(stream); //not using << to avoid extra () in output
}
};
template<typename T>
struct tuple<T>{
T first;
tuple()=default;
tuple(T t):first(t){}
operator T&(){
return first;
}
std::ostream& print(std::ostream& stream){
return stream<<first;
}
};
template<typename... T>
inline auto mk_tuple(T... t){
return tuple<T...>(t...);
}
I have operator<< overloaded this way:
template<typename... T>
std::ostream& operator<<(std::ostream &stream, tuple<T...> &out){
stream<<'(';
return out.print(stream)<<')';
}
When I try to use it this way: std::cout<<mk_tuple(1, 2, 3, "xyz", 'c'); I get
error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
However, mk_tuple(1, 2, 3, "xyz", 'c').print(std::cout) works.
(but it's unsatisfactory, because it's not obvious syntax in C++).
How should I overload operator<< to be able to use it correctly in this case?

The signature is wrong, you need:
template<typename... T>
std::ostream& operator<<(std::ostream &stream, const tuple<T...> &out){
// ^^^^^
since you are not going to modify the tuple on output. This then allows your operator to be called with constant or temporary values.
It also requires you to mark the print method as const:
std::ostream& print(std::ostream& stream) const {
// ^^^^^
Generally, google "const correctness" and learn about this essential paradigm in C++.
EDIT: Got it! Try this signature:
template<typename T, typename... U>
std::ostream& operator<<(std::ostream &stream, const tuple<T, U...> &out){
stream<<'(';
return out.print(stream)<<")";
}
as some older versions of GCC seem to have a bug that prevents them from properly deducing just T...s.

Related

Overloading ostream of vector template with iterator

Why I can't use iterator in ostream overloading?
If I use the same declaration using iterative approach it works.
Consider the following code:
template <class T>
class List {
template <class U>
friend ostream &operator<<(ostream &os, const List<U> &rhs);
private:
vector<T> v;
};
template<class U>
ostream & operator<<(ostream & os, const List<U>& rhs)
{
vector<U>::iterator it = rhs.v.begin();
return os;
}
int main()
{
List<int> list;
cout << list << endl;
return 0;
}
Note that rhs is declared as reference to const, then rhs.v will be const too, then rhs.v.begin() will return a std::vector<U>::const_iterator, which can't be converted to std::vector<U>::iterator directly.
You should use typename for dependent type names.
So change it to
typename vector<U>::const_iterator it = rhs.v.begin();
BTW: void main() should be int main().
Try with
typename vector<U>::const_iterator it = rhs.v.begin();
If your rsh is const, you should use a const_iterator

What is the right whay to overload the ostream operator, when using templates? [duplicate]

I have read couple of the questions regarding my problem on StackOverflow.com now, and none of it seems to solve my problem. Or I maybe have done it wrong...
The overloaded << works if I make it into an inline function. But how do I make it work in my case?
warning: friend declaration std::ostream& operator<<(std::ostream&, const D<classT>&)' declares a non-template function
warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
/tmp/cc6VTWdv.o:uppgift4.cc:(.text+0x180): undefined reference to operator<<(std::basic_ostream<char, std::char_traits<char> >&, D<int> const&)'
collect2: ld returned 1 exit status
The code:
template <class T>
T my_max(T a, T b)
{
if(a > b)
return a;
else
return b;
}
template <class classT>
class D
{
public:
D(classT in)
: d(in) {};
bool operator>(const D& rhs) const;
classT operator=(const D<classT>& rhs);
friend ostream& operator<< (ostream & os, const D<classT>& rhs);
private:
classT d;
};
int main()
{
int i1 = 1;
int i2 = 2;
D<int> d1(i1);
D<int> d2(i2);
cout << my_max(d1,d2) << endl;
return 0;
}
template <class classT>
ostream& operator<<(ostream &os, const D<classT>& rhs)
{
os << rhs.d;
return os;
}
This is one of those frequently asked questions that have different approaches that are similar but not really the same. The three approaches differ in who you are declaring to be a friend of your function --and then on how you implement it.
The extrovert
Declare all instantiations of the template as friends. This is what you have accepted as answer, and also what most of the other answers propose. In this approach you are needlessly opening your particular instantiation D<T> by declaring friends all operator<< instantiations. That is, std::ostream& operator<<( std::ostream &, const D<int>& ) has access to all internals of D<double>.
template <typename T>
class Test {
template <typename U> // all instantiations of this template are my friends
friend std::ostream& operator<<( std::ostream&, const Test<U>& );
};
template <typename T>
std::ostream& operator<<( std::ostream& o, const Test<T>& ) {
// Can access all Test<int>, Test<double>... regardless of what T is
}
The introverts
Only declare a particular instantiation of the insertion operator as a friend. D<int> may like the insertion operator when applied to itself, but it does not want anything to do with std::ostream& operator<<( std::ostream&, const D<double>& ).
This can be done in two ways, the simple way being as #Emery Berger proposed, which is inlining the operator --which is also a good idea for other reasons:
template <typename T>
class Test {
friend std::ostream& operator<<( std::ostream& o, const Test& t ) {
// can access the enclosing Test. If T is int, it cannot access Test<double>
}
};
In this first version, you are not creating a templated operator<<, but rather a non-templated function for each instantiation of the Test template. Again, the difference is subtle but this is basically equivalent to manually adding: std::ostream& operator<<( std::ostream&, const Test<int>& ) when you instantiate Test<int>, and another similar overload when you instantiate Test with double, or with any other type.
The third version is more cumbersome. Without inlining the code, and with the use of a template, you can declare a single instantiation of the template a friend of your class, without opening yourself to all other instantiations:
// Forward declare both templates:
template <typename T> class Test;
template <typename T> std::ostream& operator<<( std::ostream&, const Test<T>& );
// Declare the actual templates:
template <typename T>
class Test {
friend std::ostream& operator<< <T>( std::ostream&, const Test<T>& );
};
// Implement the operator
template <typename T>
std::ostream& operator<<( std::ostream& o, const Test<T>& t ) {
// Can only access Test<T> for the same T as is instantiating, that is:
// if T is int, this template cannot access Test<double>, Test<char> ...
}
Taking advantage of the extrovert
The subtle difference between this third option and the first is in how much you are opening to other classes. An example of abuse in the extrovert version would be someone that wants to get access into your internals and does this:
namespace hacker {
struct unique {}; // Create a new unique type to avoid breaking ODR
template <>
std::ostream& operator<< <unique>( std::ostream&, const Test<unique>& )
{
// if Test<T> is an extrovert, I can access and modify *any* Test<T>!!!
// if Test<T> is an introvert, then I can only mess up with Test<unique>
// which is just not so much fun...
}
}
You can't declare a friend like that, you need to specify a different template type for it.
template <typename SclassT>
friend ostream& operator<< (ostream & os, const D<SclassT>& rhs);
note SclassT so that it doesn't shadow classT. When defining
template <typename SclassT>
ostream& operator<< (ostream & os, const D<SclassT>& rhs)
{
// body..
}
This worked for me without any compiler warnings.
#include <iostream>
using namespace std;
template <class T>
T my_max(T a, T b)
{
if(a > b)
return a;
else
return b;
}
template <class classT>
class D
{
public:
D(classT in)
: d(in) {};
bool operator>(const D& rhs) const {
return (d > rhs.d);
}
classT operator=(const D<classT>& rhs);
friend ostream& operator<< (ostream & os, const D& rhs) {
os << rhs.d;
return os;
}
private:
classT d;
};
int main()
{
int i1 = 1;
int i2 = 2;
D<int> d1(i1);
D<int> d2(i2);
cout << my_max(d1,d2) << endl;
return 0;
}
I think you shouldn't make friend in the first place.
You can create a public method call print, something like this (for a non template class):
std::ostream& MyClass::print(std::ostream& os) const
{
os << "Private One" << privateOne_ << endl;
os << "Private Two" << privateTwo_ << endl;
os.flush();
return os;
}
and then, outside the class (but in the same namespace)
std::ostream& operator<<(std::ostream& os, const MyClass& myClass)
{
return myClass.print(os);
}
I think it should work also for template class, but I haven't tested yet.
Here you go:
#include <cstdlib>
#include <iostream>
using namespace std;
template <class T>
T my_max(T a, T b)
{
if(a > b)
return a;
else
return b;
}
template <class classT>
class D
{
public:
D(classT in)
: d(in) {};
bool operator>(const D& rhs) const { return d > rhs.d;};
classT operator=(const D<classT>& rhs);
template<class classT> friend ostream& operator<< (ostream & os, const D<classT>& rhs);
private:
classT d;
};
template<class classT> ostream& operator<<(ostream& os, class D<typename classT> const& rhs)
{
os << rhs.d;
return os;
}
int main()
{
int i1 = 1;
int i2 = 2;
D<int> d1(i1);
D<int> d2(i2);
cout << my_max(d1,d2) << endl;
return 0;
}

SFINAE overload operator<< to call 'print' method if it exists

I've been trying to understand SFINAE and was trying to write a simple overloaded operator << that would call the 'print' method on any class that contains such a method. I read through the answers on the question Is it possible to write a template to check for a function's existence? and tried writing:
template<class T, class = decltype(void(std::declval<T>().print), std::true_type{})>
inline std::ostream &operator<<(std::ostream &out, const T &obj) {
obj.print(out); return out; }
and
template<class T, class = decltype(void(std::declval<T>().print(std::declval<std::ostream &>())), std::true_type{})>
inline std::ostream &operator<<(std::ostream &out, const T &obj) {
obj.print(out); return out; }
but this simply doesn't work -- the compiler seems to have no problem instantiating the template for any type, so gives slews of 'ambiguous overload' errors when I try to print things like string literals...
Your "function exists" expression is incorrect. Try this:
template <typename T,
typename = decltype(
void(std::declval<T>().print(std::declval<std::ostream&>())),
std::true_type{})> // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
std::ostream & operator<<(std::ostream & out, const T & obj)
{
obj.print(out);
return out;
}
You might also consider this alternative:
template <typename T>
auto operator<<(std::ostream & out, const T & obj)
-> decltype(obj.print(out), (void)0, out)
{
obj.print(out);
return out;
}
I don't understand what you've meant your second type parameter
class = decltype(void(std::declval<T>().print), std::true_type{})
to mean. What is this supposed to evaluate to?
I think you can make it work by using the following.
#include <iostream>
#include <type_traits>
template<typename T,
typename = decltype(std::declval<const T>().print(std::cout))>
std::ostream&
operator<<(std::ostream& out, const T& obj)
{
obj.print(out);
return out;
}
struct A
{
void
print(std::ostream& out) const
{
out << "A";
}
};
int
main()
{
A a {};
std::cout << "And the winner is: " << a << std::endl;
}
It will correctly output And the winner is: A but there are probably some corner cases I've overlooked.
The expression
decltype(std::declval<const T>().print(std::cout))
will evaluate to the return type of the print(std::ostream&) const member function if such function is declared and a type error otherwise.

Friend operator in template struct raises redefinition error

Consider this code:
template<typename T,typename K>
struct A{
friend std::ostream& operator<<(std::ostream& out, K x) {
// Do some output
return out;
}
};
int main(){
A<int,int> i;
A<double,int> j;
}
It does not compile, because the two instantiations of A instantiate the operator<< two times with the same signature, so I receive this error:
test.cpp:26:25: error: redefinition of ‘std::ostream& operator<<(std::ostream&, int)’
friend std::ostream& operator<<(std::ostream& out, K x) { return out; }
^
test.cpp:26:25: error: ‘std::ostream& operator<<(std::ostream&, int)’ previously defined here
How to fix this? How can I have a friend operator in a template, when that operator might have the same signature for two different instantiations? How can I solve this without triggering a redefinition error?
I don't really see any use in declaring a friend like this, nevertheless this is how you can do it:
template<typename T, typename K>
struct A{
template<typename L>
friend std::ostream& operator<<(std::ostream& out, L const &x);
};
template<typename T>
std::ostream& operator<<(std::ostream& out, T const &x) {
// ...
return out;
}
LIVE DEMO
Edit:
Another option probably closer to what you want will be:
template<typename T>
std::ostream& operator<<(std::ostream& out, T const &x);
template<typename T, typename K>
struct A{
friend std::ostream& operator<<<K>(std::ostream& out, K const &x);
};
template<typename T>
std::ostream& operator<<(std::ostream& out, T const &x) {
// ...
return out;
}
LIVE DEMO
But really not sure why you want this. IMHO your design has serious flaws.
Your question is faulty in the following way.
Assuming it's a stub and your class has something private such that it needs to declare a friend, the purpose of doing so is that the friend is external to the class itself but has access to what is private within it.
In your case you are declaring streaming functions of a parameter as a friend. That is fine. It means if someone creates a class Bar and wants to define how a Bar is streamed their implementation may access anything in A<T,Bar> for any type T.
The clashing of your template with operator<<( ostream&, int) is not actually a problem as the compiler knows which one to pick. It will always pick the exact match non-templated over the templated one. Your problem is that you have 2 templated ones and the compiler cannot pick between them because they are both equally valid.
Maybe something like this is what you are really trying to achieve
template< typename X, typename Y >
struct A
{
friend void a_print( std::ostream& Y const & ); // foo external function with Y as parameter, can access this
};
std::ostream & operator<<( std::ostream & out, Bar const& bar )
{
a_print( out, bar );
return out;
}
void a_print( Bar const& bar, std::ostream & out )
{
// implement and call private members of A<Foo, Bar>
return out;
}
You could make the streaming template a friend and implement the one for Bar to stream using a specific implementation.
Factor the method out to a base class:
template <typename K>
struct ABase
{
friend std::ostream& operator<<(std::ostream& out, K x) {
// Do some output
return out;
}
};
template <typename T,typename K>
struct A : public ABase<K>
{};
If you want to stream an A object, the proper signature should have been
template<typename T,typename K>
struct A{
friend std::ostream& operator<<(std::ostream& out, const A& x) {
// Do some output
return out;
}
};
taking an A<T,K> for which you can access private members, not a K.
If there is no way to get an A from K (and cannot be if K is int), making <<(ostream&,K) a friend of A gives no benefits.

Cast to integral type or other type cannot appear in a constant expression

Ideone testcase: http://ideone.com/lzepF
Code:
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <stdint.h>
template <typename T> std::ostream& write(std::ostream& os, T const& x);
template <typename T> std::istream& read(std::istream& is, T& x);
template <typename T, typename U> std::ostream& write(std::ostream& os, std::pair<T, U> const& rh);
template <typename T, typename U> std::istream& read(std::istream& is, std::pair<T, U>& rh);
template <typename T> std::ostream& writeContainer(std::ostream& os, T const& rh);
template <typename T, typename U> std::ostream& write(std::ostream& os, std::map<T, U> const& rh);
template <typename T, typename U> std::istream& read(std::istream& is, std::map<T, U> rh);
template <typename T> std::ostream& write(std::ostream& os, std::vector<T> const& rh);
template <typename T> std::istream& read(std::istream& is, std::vector<T>& rh);
template <typename T>
std::ostream& write(std::ostream& os, T const& x){
static_assert(!std::is_pointer<T>(), "That's a pointer, you probably don't want to write that");
static_assert(std::is_pod<T>(), "That's not a POD: can't write it");
os.write(reinterpret_cast<const char *>(&x), sizeof(T));
return os;
}
template <typename T>
std::istream& read(std::istream& is, T& x){
static_assert(!std::is_pointer<T>(), "That's a pointer, you probably don't want to read that");
static_assert(std::is_pod<T>(), "That's not a POD: can't read it");
is.read(reinterpret_cast<char *>(&x), sizeof(T));
return is;
}
template <typename T, typename U>
std::ostream& write(std::ostream& os, std::pair<T, U> const& rh){
write(os, rh.first);
write(os, rh.second);
return os;
}
template <typename T, typename U>
std::istream& read(std::istream& is, std::pair<T, U>& rh){
read(is, rh.first);
read(is, rh.second);
return is;
}
template <typename T>
std::ostream& writeContainer(std::ostream& os, T const& rh){
uint32_t size = std::distance(rh.begin(), rh.end());
write(os, size);
for(auto it = rh.begin(); it != rh.end(); ++it){
write(os, *it);
}
return os;
}
template <typename T>
std::istream& readContainer(std::istream& is, T& rh){
uint32_t size;
read(is, size);
std::insert_iterator<T> it(rh, rh.end());
for(uint32_t i=0; i<size; ++i) {
typename T::value_type x;
read(is, x);
it = x;
}
return is;
}
template <typename T, typename U>
std::ostream& write(std::ostream& os, std::map<T, U> const& rh){
return writeContainer(os, rh);
}
template <typename T, typename U>
std::istream& read(std::istream& is, std::map<T, U> rh){
return readContainer(is, rh);
}
template <typename T>
std::ostream& write(std::ostream& os, std::vector<T> const& rh){
return writeContainer(os, rh);
}
template <typename T>
std::istream& read(std::istream& is, std::vector<T>& rh){
return readContainer(is, rh);
}
int main(){
{
std::stringstream s;
std::vector<int> x = {0, 1, 2, 3};
write(s, x);
}
{
std::stringstream s;
std::map<int, int> x = {{0, 0}, {1, 2}, {2, 4}, {3, 6}};
write(s, x);
}
return 0;
}
Errors:
prog.cpp: In function 'std::ostream& write(std::ostream&, const T&) [with T = unsigned int, std::ostream = std::basic_ostream<char>]':
prog.cpp:57:2: instantiated from 'std::ostream& writeContainer(std::ostream&, const T&) [with T = std::vector<int>, std::ostream = std::basic_ostream<char>]'
prog.cpp:92:30: instantiated from 'std::ostream& write(std::ostream&, const std::vector<T>&) [with T = int, std::ostream = std::basic_ostream<char>]'
prog.cpp:104:13: instantiated from here
prog.cpp:29:11: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
prog.cpp:29:11: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
prog.cpp: In function 'std::ostream& write(std::ostream&, const T&) [with T = int, std::ostream = std::basic_ostream<char>]':
prog.cpp:60:3: instantiated from 'std::ostream& writeContainer(std::ostream&, const T&) [with T = std::vector<int>, std::ostream = std::basic_ostream<char>]'
prog.cpp:92:30: instantiated from 'std::ostream& write(std::ostream&, const std::vector<T>&) [with T = int, std::ostream = std::basic_ostream<char>]'
prog.cpp:104:13: instantiated from here
prog.cpp:29:11: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
prog.cpp:29:11: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
Since we have no line numbers, :( , it complains about the FIRST write(s, x), and seems to me complain about the reinterpret_cast<const char*>(const unsigned int*) but I'm pretty sure that should be legal.
What's going wrong?
You are using the is_pod and is_pointer incorrectly. You need to change it to the following:
static_assert(!std::is_pointer<T>::value, "That's a pointer, you probably don't want to write that");
static_assert(std::is_pod<T>::value, "That's not a POD: can't write it");
You can test with this:
write(std::cout, 1);
int *p = 0;
write(std::cout, p);
C++03 Standard 5.19 "Constant expressions", Para 1 states:
In several places, C++ requires expressions that evaluate to an integral or enumeration constant: as array bounds (8.3.4, 5.3.4), as case expressions (6.4.2), as bit-field lengths (9.6), as enumerator initializers (7.2), as static member initializers (9.4.2), and as integral or enumeration non-type template arguments (14.3).
constant-expression: conditional-expression
An integral constant-expression can involve only literals (2.13), enumerators, const variables or static data members of integral or enumeration types initialized with constant expressions (8.5), non-type template parameters of integral or enumeration types, and sizeof expressions. Floating literals (2.13.3) can appear only if they are cast to integral or enumeration types. Only type conversions to integral or enumeration types can be used. In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used.
The code statements:
static_assert(!std::is_pointer(), "That's a pointer, you probably don't want to write that");
static_assert(std::is_pod(), "That's not a POD: can't write it");
break this rule and hence the compiler complains.