Qustion on templates

I

Ioannis Vranos

I am experimenting with a template class:


#include <iostream>




template <class Value_type>
class only
{
Value_type m_value;

template<class U> only(U);


public:

typedef Value_type value_type;

typedef Value_type& reference;

only(value_type value): m_value( value ) { }

value_type value() const { return m_value; }

std::eek:stream & operator<<(std::eek:stream &);
};




template <class Value_type>
std::eek:stream & only<Value_type>::eek:perator<<(std::eek:stream &objOstream)
{
objOstream<< m_value;

return objOstream;
}


int main()
{
using namespace std;

only<int> obj= 10;

obj.operator<<(cout); // This works
}


Why cout<< obj; doesn't work? Is it a compiler issue?
 
V

Vladyslav Lazarenko

I am experimenting with a template class:

#include <iostream>

template <class Value_type>
class only
{
        Value_type m_value;

         template<class U> only(U);

        public:

        typedef Value_type value_type;

        typedef Value_type& reference;

        only(value_type value): m_value( value ) { }

        value_type value() const { return m_value; }

         std::eek:stream & operator<<(std::eek:stream &);

};

template <class Value_type>
std::eek:stream & only<Value_type>::eek:perator<<(std::eek:stream &objOstream)
{
        objOstream<< m_value;

        return objOstream;

}

int main()
{
     using namespace std;

     only<int> obj= 10;

     obj.operator<<(cout);  // This works

}

Why cout<< obj; doesn't work? Is it a compiler issue?

Not at all, it is an issue of understanding what and how is being
called.

1) Using "class" in template parameter definition is deprecated.
2) Main function should always return a value.
3) Operator << is std::eek:stream's operator and not an only's template.
Therefore it should be defined as "free" function and not a member of
"only" template.

I am impressed. How can you propose to add something to the C++
standard without understanding of basic features of that language?
 
I

Ioannis Vranos

Vladyslav said:
Not at all, it is an issue of understanding what and how is being
called.

1) Using "class" in template parameter definition is deprecated.


Since when?

2) Main function should always return a value.


Not true in C++98/03.


3) Operator << is std::eek:stream's operator and not an only's template.
Therefore it should be defined as "free" function and not a member of
"only" template.


The truth is that any binary operator, can be defined both as a member function of a class and as a regular
function.


I am impressed. How can you propose to add something to the C++
standard without understanding of basic features of that language?


All people make mistakes, so I will not criticise you for this.
 
T

Thomas J. Gritzan

Ioannis said:
I am experimenting with a template class:


#include <iostream> [...]
template <class Value_type>
std::eek:stream & only<Value_type>::eek:perator<<(std::eek:stream &objOstream)
{
objOstream<< m_value;

return objOstream;
}


int main()
{
using namespace std;

only<int> obj= 10;

obj.operator<<(cout); // This works
}


Why cout<< obj; doesn't work? Is it a compiler issue?

You have defined the operator to be callable as

obj.operator<<(cout); or as
obj << cout;

If you define a binary member operator, the class, the operator is
defined in, will always be on the left side.

You'ld have to define
std::eek:stream::eek:perator<<(only<Value_type>),
but since you can't redefine std::eek:stream, the only way is to define the
operator<< as free function.

You can define it as friend function inside the class definition, though:

class example
{
// ...

friend std::eek:stream& operator<<(std::eek:stream& os, example const& e)
{
// op<< implementation
}
};
 
I

Ioannis Vranos

Ioannis said:
Since when?




Not true in C++98/03.





The truth is that any binary operator, can be defined both as a member
function of a class and as a regular function.





All people make mistakes, so I will not criticise you for this.


Check my public transcript here: http://www.brainbench.com

Transcript ID: 2414700
 
I

Ioannis Vranos

Ioannis said:
Why cout<< obj; doesn't work? Is it a compiler issue?


OK, I had some time to implement templates and had gotten a bit rusty. The mistake is obvious, and yes indeed
obviously this operator<< must be defined as a regular function (friend or not).


The code corrected:


#include <iostream>


template <class Value_type>
class only;


// Implemented as non-friend, because it was not needed.
template <class Value_type>
inline std::eek:stream & operator<<(std::eek:stream &objOstream, const only<Value_type> &obj)
{
using namespace std;

return objOstream<< obj.value();
}


template <class Value_type>
class only
{
Value_type m_value;

template<class U> only(U);


public:

typedef Value_type value_type;

typedef Value_type& reference;

only(value_type value): m_value( value ) { }

value_type value() const { return m_value; }

};



int main()
{
using namespace std;

only<int> obj= 10;

cout<< obj<< endl;
}
 
R

red floyd

Ioannis said:
OK, I had some time to implement templates and had gotten a bit rusty.
The mistake is obvious, and yes indeed obviously this operator<< must be
defined as a regular function (friend or not).


The code corrected:


#include <iostream>


template <class Value_type>
class only;


// Implemented as non-friend, because it was not needed.
template <class Value_type>
inline std::eek:stream & operator<<(std::eek:stream &objOstream, const
only<Value_type> &obj)
{
using namespace std;
unnecessary. you use nothing from std in this function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top