The following code:
typedef void HELPER;
const HELPER* helper = _helper;
inline ostream& operator <<(ostream& out, const HELPER* arg)
{ out << (const char*)(arg); return out; }
Blows up if I attempt a
cout << helper;
Specifically, I get:
main.cpp:35:28: error: use of overloaded operator '<<' is ambiguous
(with operand types 'basic_ostream >' and 'const HELPER *' (aka 'const void *'))
and it lists a few candidates:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:207:0: note: candidate function
basic_ostream& operator<<(const void* __p);
^
main.cpp:25:17: note: candidate function
inline ostream& operator <<(ostream& out, const HELPER* arg)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:195:20: note: candidate function
basic_ostream& operator<<(bool __n);
^
I'm a little surprised that my typedef isn't invoking a stronger type matching here. How can I get this operator overload running?
EDIT: Further clarification, the purpose of this code is that I am dual-targeting a set of Arduino libraries. They manage their strings frequently with:
typedef void __FlashStringHelper;
void showHelp(const __FlashStringHelper* helpText)
{
Serial.print(helpText);
}
I like iostream and planned on this dual target, so I overloaded << on Serial object and made the previous into (this is the oversimplified version, for example)
#define cout Serial
void showHelp(const __FlashStringHelper* helpText)
{
cout << helpText;
}
Now I want to actually target real iostream for a different arch, but the old Arduino code can't vary (much) from its __FlashStringHelpers. That's where I'm at
typedef doesn't create types it aliases them,
inline ostream& operator <<(ostream& out, const HELPER* arg)
is equivalent to
inline ostream& operator <<(ostream& out, const void* arg)
Maybe you wanted to create a type named HELPER
class HELPER{};
As Zekian answered your question this here is something that may be of use to you or help you to achieve what you are trying to do.
#include <iostream>
template <class T>
class Helper {
private:
T obj_;
public:
explicit Helper<T>( T obj ) : obj_(obj) {}
public:
T getObj() const { return obj_; }
void setObj( T obj ) { obj_ = obj; }
template<class U>
inline friend std::ostream& operator<< ( std::ostream& out, const Helper<U>& rhs );
};
template<class U>
std::ostream& operator<< ( std::ostream& out, const Helper<U>& rhs ) {
return out << rhs.obj_;
}
int main() {
Helper<int> helper( 3 );
std::cout << helper << std::endl;
return 0;
}
It is a wrapper class template with an overloaded ostream operator<<. This will work for integral and atomic types. If you pass another struct or class object then you will have to define other overloaded ostream operators for them.
Example - Same Class Template, but this time using a class or struct.
#include <iostream>
template <class T>
class Helper {
private:
T obj_;
public:
explicit Helper<T>( T obj ) : obj_(obj) {}
public:
T getObj() const { return obj_; }
void setObj( T obj ) { obj_ = obj; }
template<class U>
inline friend std::ostream& operator<< ( std::ostream& out, const Helper<U>& rhs );
};
template<class U>
std::ostream& operator<< ( std::ostream& out, const Helper<U>& rhs ) {
return out << rhs.obj_;
}
struct Staff {
int employees = 4; // Default to 4
};
int main() {
Staff staff;
Helper<Staff> helper( staff );
std::cout << helper << std::endl; // will not compile
return 0;
}
To fix this ostream will need an overloaded operator for Staff object
template <class T>
class Helper {
private:
T obj_;
public:
explicit Helper<T>( T obj ) : obj_(obj) {}
public:
T getObj() const { return obj_; }
void setObj( T obj ) { obj_ = obj; }
template<class U>
inline friend std::ostream& operator<< ( std::ostream& out, const Helper<U>& rhs );
};
template<class U>
std::ostream& operator<< ( std::ostream& out, const Helper<U>& rhs ) {
return out << rhs.obj_;
}
struct Staff {
int employees = 4;
inline friend std::ostream& operator<< ( std::ostream& out, const Staff& rhs );
};
std::ostream& operator<<( std::ostream& out, const Staff& rhs ) {
return out << rhs.employees;
}
int main() {
Staff staff;
Helper<Staff> helper( staff ); // Default to 4
std::cout << helper << std::endl; // Will Print 4
// To Change Staff's Employee count for the helper wrapper do this:
staff.employees = 12; // Change To 12
helper.setObj( staff ); // pass the changed struct back into helper
std::cout << helper3 << std::endl; // Will Now Print 12
// And For Other Default Types
Helper<int> helper2( 3 );
std::cout << helper2 << std::endl;
Helper<float> helper3( 2.4f );
std::cout << helper3 << std::endl;
return 0;
}
Related
Here's the functionality I am expecting to achieve:
darray<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
std::cout << a << std::endl; // displays: {1, 2, 3}
My implementation:
template <typename T>
class darray
{
private:
long m_capacity;
long m_size;
T* m_data;
void resize();
public:
// constructors & destructors
darray();
// operations
void push_back(T);
std::ostream& print(std::ostream&) const;
template<typename U> friend std::ostream& operator<<(std::ostream& os, U const& ar);
};
template<typename T>
std::ostream& darray<T>::print(std::ostream& os) const
{
os << "{ ";
for (size_t i = 0; i < m_size; i++)
{
os << m_data[i] << ", ";
if ( i == m_size - 1 )
os << m_data[i];
}
os << " }\n";
return arr;
}
template<typename U>
std::ostream& operator<<(std::ostream& os, U const& obj)
{
return obj.print(os);
}
produces an error:
error: ambiguous overload for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const char [66]’)
But, when I change the parameter of operator<< to accept a darray<U> instead , it works fine:
template<typename U>
std::ostream& operator<<(std::ostream& os, darray<U> const& obj)
{
return obj.print(os);
}
What am I missing here?
Update:
I also tried doing this, changing the parameter to darray<U> type in the definition and the implementation, but it still produces the same error:
template <typename T>
class darray
{
private:
long m_capacity;
long m_size;
T* m_data;
void resize();
public:
// constructors & destructors
darray();
// operations
void push_back(T);
std::ostream& print(std::ostream&) const;
template<typename U> friend std::ostream& operator<<(std::ostream& os, darray<U> const& ar);
};
template<typename T>
std::ostream& darray<T>::print(std::ostream& os) const
{
os << "{ ";
for (size_t i = 0; i < m_size; i++)
{
os << m_data[i] << ", ";
if ( i == m_size - 1 )
os << m_data[i];
}
os << " }\n";
return os;
}
template<typename U>
std::ostream& operator<<(std::ostream& os, darray<U> const& obj)
{
return obj.print(os);
}
Friend functions in template classes have to be defined inside the class declaration. This is the only way I have found to have the friend function to correctly accept an instance of the templated class with the expected template.
So here I would write:
...
friend std::ostream& operator<<(std::ostream& os, darray<T> const& ar) {
ar.print(os);
return os;
}
...
But beware: your class contains a raw pointer to allocated memory which is probably deleted in the class destructor. Per the rule of five, you must explicitely declare (or delete) the copy/move constructors and assignment operators.
In your darray<T>::print function, if you change
os << m_data[i] << ", ";
to
os << m_data[i];
os << ", ";
then the compiler doesn't complain and it works fine. I don't know why.
This question already has answers here:
overloading friend operator<< for template class
(5 answers)
Closed 1 year ago.
I am trying to define my operator overloading outside the class like this:
template <typename Type>
class myClass
{
...
friend std::ostream& operator << (std::ostream&, const myClass&);
friend std::istream& operator >> (std::istream&, myClass&);
...
}
template <typename Type>
std::ostream& myClass<Type> :: operator << (std::ostream& out, const myClass<Type>& other) { ... }
template <typename Type>
std::istream& myClass<Type> :: operator >> (std::istream& in, myClass<Type>& other) { ... }
My problem is that I get an error saying something like "class template class has no member operator<<" and I do not understand how or why is this happening.
For starters the friend functions are not member functions of the class where they are declared.
So these declarations in any case are incorrect.
template <typename Type>
std::ostream& myClass<Type> :: operator << (std::ostream& out, const myClass<Type>& other) { ... }
template <typename Type>
std::istream& myClass<Type> :: operator >> (std::istream& in, myClass<Type>& other) { ... }
Secondly within the class definition you declared non-template friend functions.
Either you need to provide their definitions within the class definition. Or for each potential template argument of the class you have to define the friend functions outside the class.
Here is a demonstration program that shows how to define a non-template friend function within the class template definition.
#include <iostream>
template <typename T>
class A
{
private:
T t;
public:
A( const T &t ) : t( t ) {}
friend std::ostream &operator <<( std::ostream &os, const A &a )
{
return os << a.t;
}
};
int main()
{
std::cout << A<int>( 10 ) << '\n';
std::cout << A<const char *>( "Hello" ) << '\n';
}
The program output is
10
Hello
And here is a demonstration program that shows how to define the friend functions outside the class template definition. That is for each used specialization of the class template you need to define the non-template friend functions.
#include <iostream>
template <typename T>
class A
{
private:
T t;
public:
A( const T &t ) : t( t ) {}
friend std::ostream &operator <<( std::ostream &os, const A &a );
};
std::ostream &operator <<( std::ostream &os, const A<int> &a )
{
return os << a.t;
}
std::ostream &operator <<( std::ostream &os, const A<const char *> &a )
{
return os << a.t;
}
int main()
{
std::cout << A<int>( 10 ) << '\n';
std::cout << A<const char *>( "Hello" ) << '\n';
}
An alternative approach is to declare template friend functions. For example
#include <iostream>
template <typename T>
class A
{
private:
T t;
public:
A( const T &t ) : t( t ) {}
template <typename T>
friend std::ostream &operator <<( std::ostream &os, const A<T> &a );
};
template <typename T>
std::ostream &operator <<( std::ostream &os, const A<T> &a )
{
return os << a.t;
}
int main()
{
std::cout << A<int>( 10 ) << '\n';
std::cout << A<const char *>( "Hello" ) << '\n';
}
I'm implementing a dictionary in C++ using a binary tree, each node in my tree has a key(int), item(string) and left and right child.
During this implementation, I overloaded the ostream operator for my BinaryTree class to print out the contents of the tree.
Additionally, I overloaded the ostream to work with Node pointers that would then print out the key and the item of that node.
This worked fine. However when I tried to then make the tree a template to work with any type for my key or item overloading these operators became more difficult.
I isolated the problem to make it easier to work on, additionally, I have tried playing around with both a node and a node pointer to see if I can get one to work without the other.
Here is the class I have made to test the problem, this class is not templated and works fine.
test.h
class myClass
{
public:
using Key = int;
myClass(Key);
friend std::ostream & operator<<(std::ostream &, const myClass &);
private:
struct Thing {
Key data;
Thing();
Thing(Key);
};
Thing* B;
Thing A;
void disp(std::ostream &) const;
friend std::ostream & operator<<(std::ostream &, myClass::Thing);
friend std::ostream & operator<<(std::ostream &, myClass::Thing *);
};
test.cpp
myClass::Thing::Thing(Key Num) { data = Num; }
myClass::myClass(Key Num)
{
A = Thing(Num); B = &A;
}
void myClass::disp(std::ostream & os) const
{
os << A << std::endl; os << B << std::endl;
}
std::ostream & operator<<(std::ostream & os, const myClass & c)
{
c.disp(os); return os;
}
std::ostream & operator<<(std::ostream & os, myClass::Thing th)
{
os << th.data; return os;
}
std::ostream & operator<<(std::ostream & os, myClass::Thing *th)
{
os << th->data; return os;
}
With this class, I can easily make an instance of my class and std::cout it giving the output as expected.
Then turning this class into a template:
template <class T> class myTemplate
{
public:
using Key = T;
myTemplate(Key);
template<class A>
friend std::ostream & operator<<(std::ostream &, const myTemplate<A> &);
private:
struct Thing;
Thing A;
Thing* B;
void disp(std::ostream &) const;
template <class A> friend std::ostream & operator<<(std::ostream &, typename myTemplate<A>::Thing);
template <class A> friend std::ostream & operator<<(std::ostream &, typename myTemplate<A>::Thing *);
};
template <class T> struct myTemplate<T>::Thing
{
T data;
Thing() = default;
Thing(Key);
};
//Create new thing A with B a pointer to A
template <class T> myTemplate<T>::myTemplate(Key Num)
{
A = Thing(Num);
B = &A;
}
//Displays Node A & B
template <class T> void myTemplate<T>::disp(std::ostream & os) const
{
os << A << std::endl; os << B << std::endl;
}
template <class T> myTemplate<T>::Thing::Thing(Key Num)
{
data = Num;
}
//Overloading << will call disp function, in turn print A & B to stream
template<class T> std::ostream & operator<<(std::ostream & os, const myTemplate<T> & c)
{
c.disp(os); return os;
}
//Output a to stream
template <class A> std::ostream & operator<<(std::ostream & os, typename myTemplate<A>::Thing th)
{
os << th.data; return os;
}
//Output a to stream
template <class A> std::ostream & operator<<(std::ostream & os, typename myTemplate<A>::Thing *th)
{
os << th->data; return os;
}
However with myTemplate when I tried in the main():
myTemplate Template(5);
cout << Template;
The code will not compile as I get the error:
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'const myTemplate<std::string>::Thing' (or there is no acceptable conversion)
Additionally commenting out the line:
os << A << std::endl;
So only B is being outputted to the stream, the code will compile. However the data of B is not outputted, only the memory address of B.
I have noticed that using breakpoints when trying to output B the code does not even use the overload function I defined. This is not the case for the non-templated class as the overloads I defined are used for both A and B.
So what is the correct way to overload the ostream operator to work for the struct member?
Apologises for the long winded question, felt I should include what I had determined myself.
Since template implementation is going to be in a single translate unit(header file) itself, you are not going to gain anything more, by tearing the things apart. Therefore, keep the definitions and non-member functions inside the class itself, which will provide you with much clear code and also improve the readability of the template class:
See this
#include <iostream>
template <class T> class myTemplate
{
public:
using Key = T;
private:
struct Thing
{
T data;
Thing() = default;
Thing(Key Num) : data(Num) {}
};
Thing A;
Thing* B = nullptr;
public:
myTemplate(Key Num) : A(Thing(Num)), B(&A) {}
friend std::ostream & operator<<(std::ostream& out, const myTemplate &obj)
{
return out << obj.A << std::endl << obj.B << std::endl;
}
friend std::ostream & operator<<(std::ostream& out, typename myTemplate::Thing thing)
{
return out << thing.data;
}
friend std::ostream & operator<<(std::ostream& out, typename myTemplate::Thing *thing)
{
return out << thing->data;
}
};
int main()
{
myTemplate<int> obj(10);
std::cout << obj;
return 0;
}
Update: If the final aim of providing that two operator<< overloads to the struct Thing is just to conveniently call in the operator<< of the myTemplate class, then you do not need that, rather simply print the data directly in operator<< of the myTemplate class. That will again reduce significate amount of codes(if that is the case!).
Nevertheless, now you can provide a specialization for the non-member(friend) function(i.e, operator<<) for the myTemplate class, as follows:
template <class T> class myTemplate; // forward declaration
template <class T> std::ostream& operator<<(std::ostream& out, const myTemplate<T> &obj);
template <class T> class myTemplate
{
private:
using Key = T;
private:
template <class Type = T> struct Thing
{
Type data;
Thing() = default;
Thing(const Key Num) : data(Num) {}
};
private:
Thing<> A;
Thing<> *B = nullptr;
public:
myTemplate(const Key Num) : A(Thing<>(Num)), B(&A) {}
friend std::ostream & operator<<<>(std::ostream& out, const myTemplate &obj);
};
template <class T> std::ostream & operator<<(std::ostream& out, const myTemplate<T> &obj)
{
return out << obj.A.data << std::endl << obj.B->data << std::endl;
}
I am trying to overload "<<" operator to call 2 methods but compiler gives me an error:
invalid initialization of non-const reference of type 'std::ostream&'
{aka 'std::basic_ostream<char>&' from an rvalue of type 'void'
return v.init();
Here is my class definition:
template<class T>
class Vector
{
private:
std::vector<T> _Vec;
public:
void fillVector();
void printElements();
void init() { fillVector(); printElements(); }
friend std::ostream& operator<<(std::ostream& os, Vector& v) {
return v.init();
};
How can I fix it?
You're doing that wrong.
This template is misleading. Its name is terrible.
This extra methods: fillVector, printElements, init are confusing (what exactly they should do?).
Most probably printElements is missing std::ostream& stream argument (and maybe return type).
You didn't described what kind of functionality you are trying to implement. Most probably this is what you need:
template<class T>
class PrintContainer
{
public:
PrintContainer(const T& container)
: mContainer { container }
{}
std::ostream& printTo(std::ostream& stream) const {
// or whatever you need here
for (const auto& x : mContainer) {
stream << x << ", ";
}
return stream;
}
private:
const T& mContainer;
};
template<class T>
std::ostream& operator<<(std::ostream& os, const PrintContainer<T>& p) {
return p.printTo(os);
}
I'm working on a tiny compiler, and I use boost::variant<bool, ClassA> infoto store the information of each node.
boost::variant will automatically call the correct << operator of a specific type when I call std::cout<< node.info;
However, since the built-in formatting function of ostream doesn't satisfy my requirement(print #t instead of 1 if node.info==true and print "string" instead of string), new types of bool/string should be introduced.
I want to implement a template class Wrapper<T>, which behaves just like T(because there are lots of old code) and provides the interface of <<.
At first, the following version was implemented:
template<typename T> class Wrapper : public T
{
public:
template<typename ... U> Wrapper(const U& ... a):T(a...) {}
friend std::ostream& operator <<(std::ostream& o, const Wrapper<T>& w);
};
This version works well for std::string, but when T=bool, since built-in types cannot be inherited, an error will raise.
My current workaround is to use partial specialization:
template<typename T, bool ISCLASS= std::is_class<T>::value> class Wrapper;
template<typename T> class Wrapper<T, false>
{
private:
T inner;
public:
template<typename ... U> Wrapper(const U& ... a): inner(a...) {}
//Wrap the operators (= + += ...)
template<typename U> Wrapper<T> operator !() { Wrapper<T> res(*this); res.inner=!res.inner; return res; }
operator T() const{ return inner; }
friend std::ostream& operator <<(std::ostream& o, const Wrapper<T>& w);
};
template<typename T> class Wrapper<T, true> : public T
{
public:
template<typename ... U> Wrapper(const U& ... a):T(a...) {}
friend std::ostream& operator <<(std::ostream& o, const Wrapper<T>& w);
};
Obviously it is not a perfect solution because I have to wrap every operators of bool or any other built-in types.
Any help would be appreciated.
Thanks.
Could we consider something simpler?
Create a simple wrapper, using reference or pointer.
template <class T>
struct MyFormattingWrapper
{
const T& nucleus;
};
And then a factory function for it.
template <class T>
MyFormattingWrapper<T> my_formatting(const T& n)
{
return MyFormattingWrapper<T>{ n };
}
And then, you can specialize the formatting for each type.
std::ostream& operator << (std::ostream& o, const MyFormattingWrapper<int>& w)
{
return o << "int:" << w.nucleus;
}
std::ostream& operator << (std::ostream& o, const MyFormattingWrapper<std::string>& w)
{
return o << "std::string:" << w.nucleus;
}
int main()
{
std::cout << my_formatting(123) << std::endl;
std::cout << my_formatting(std::string{ "abc" }) << std::endl;
}
Update:
C-string may be a special case. But it is not difficult.
struct MyFormattingWrapper_c_string
{
const char* const nucleus;
};
MyFormattingWrapper_c_string my_formatting(const char* n)
{
return MyFormattingWrapper_c_string{ n };
}
MyFormattingWrapper_c_string my_formatting(char* n)
{
return MyFormattingWrapper_c_string{ n };
}
std::ostream& operator << (std::ostream& o, const MyFormattingWrapper_c_string& w)
{
return o << "c-string:" << w.nucleus;
}
Nicky C's answer is great, but has an issue with partial specialization of functions not being OK. This means you can't produce a version that works on general vectors like this:
template<typename T>
std::ostream& operator << (std::ostream& o, const MyFormattingWrapper<std::vector<T>>& vec)
{
o << "vec:[ "
for(auto v : vec) {
o<<my_formatting(v);
o<<" ";
}
return o<<"]"
}
You can get around this by putting the core of the specialized output into the MyFormattingWrapper classes and having only one operator<<
// The default one
template <class T> struct MyFormattingWrapper {
const T& nucleus;
ostream& output(ostream & os) {
return os<<nucleus;
}
};
// Specialized for string
template <> struct MyFormattingWrapper<std::string> {
const std::string& nucleus;
ostream& output(ostream & os) {
return os<<"string:"<<nucleus;
}
};
// Specialized for vector
template <class T> struct MyFormattingWrapper<std::vector<T>> {
const std::vector<T>& nucleus;
ostream& output(ostream & os) {
os<<"vec:[";
for(auto & v: nucleus) {
os<<my_formatting(v)<<" ";
}
return os<<"]";
}
};
// Now there's just one of these, so partial template
// specialization doesn't cause us any problems
template<typename T>
std::ostream& operator << (std::ostream& os, const MyFormattingWrapper<T>& w) {
return w.output(os);
}
Perhaps I better make the follow-up regarding boost::variant another answer.
Firstly, learning from #MichaelAnderson, and considering the interoperability with boost::variant, I would like to improve the design of the wrapper. We add a constructor to enable type conversion for from the nucleus type to the wrapper type.
template <class T>
class MyFormatting;
template <class T>
MyFormatting<T> my_formatting(const T& n)
{
return MyFormatting <T>{n};
}
// int
template <>
class MyFormatting<int>
{
private:
const int& nucleus;
public:
MyFormatting(const int& n) : nucleus(n) {}
friend std::ostream& operator << (std::ostream& os, const MyFormatting& w)
{
return os << "int:" << w.nucleus;
}
};
// std::string
template <>
class MyFormatting<std::string>
{
private:
const std::string& nucleus;
public:
MyFormatting(const std::string& n) : nucleus(n) {}
friend std::ostream& operator << (std::ostream& os, const MyFormatting& w)
{
return os << "std::string:" << w.nucleus;
}
};
// c-string
template <>
class MyFormatting<char*>
{
private:
const char* nucleus;
public:
MyFormatting(const char* n) : nucleus(n) {}
friend std::ostream& operator << (std::ostream& os, const MyFormatting& w)
{
return os << "c-string:" << w.nucleus;
}
};
MyFormatting<char*> my_formatting(const char* n)
{
return MyFormatting<char*>{n};
}
// std::vector
template <class T>
class MyFormatting<std::vector<T>>
{
private:
const std::vector<T>& nucleus;
public:
MyFormatting(const std::vector<T>& n) : nucleus(n) {}
friend std::ostream& operator << (std::ostream& os, const MyFormatting& w)
{
os << "std::vector:[";
for (const auto& x : w.nucleus)
{
os << x << " ";
}
os << "]";
return os;
}
};
Next, let's use the wrapper with boost::variant. The constructor of the wrapper enables conversion between variant of nuclues types to variant of the wrappers.
boost::variant<int, std::string> var_int = 50;
boost::variant<int, std::string> var_str = "fifty";
boost::variant<MyFormatting<int>, MyFormatting<std::string>> var_fmt_int = var_int;
boost::variant<MyFormatting<int>, MyFormatting<std::string>> var_fmt_str = var_str;
std::cout << var_int << " " << var_str << std::endl;
std::cout << var_fmt_int << " " << var_fmt_str << std::endl;
But boost::variant<MyFormatting<int>, MyFormatting<std::string>> spells too long. We can make it shorter.
template <class... T>
using Variant_Formatting_t = boost::variant < MyFormatting<T>... > ;
std::cout << Variant_Formatting_t<int, std::string>{var_int} << " " << Variant_Formatting_t<int, std::string>{var_str} << std::endl;
Since boost::variant use macro-template metaprogramming to emulate variadic template instead of using C++11 variadic template, I cannot make it cleaner using type deduction. This is the furthest I can get to.