Re: operator<< template declaration problem

P

porschberg

Of course this works, but then I have to update my template header
file every time I instantiate a new template.
This is not very smart. Is there no better way ?
 
T

tom_usenet

Of course this works, but then I have to update my template header
file every time I instantiate a new template.
This is not very smart. Is there no better way ?

No, implicit instantation automatically creates the required
instantiations - this is why putting template definitions in the
header is the norm, since it requires no special maintenance. It does
however mean that code that uses the template is dependent on its
implementation.

But there is another way. Read down.

Make the above a non-template. You aren't going to be able to deduce T
in a call anyway. Consider this:

struct Foo
{
typedef int row;
};

struct Bar
{
typedef int row;
};

otl_stream << 5; //calls operator<< <Foo> or <Bar>!?

You can't deduce template arguments from nested types. So change your
friend declaration to:

//non-template friend:
friend otl_stream& operator<<(otl_stream &output, const typename
T::row& row);



In update.cc:

otl_stream& operator<<(otl_stream &output, const int& row)
{
//definition
}

otl_stream& operator<<(otl_stream &output, const whatever& row)
{
//definition
}

etc. Obviously, you could use another template in the .cc file and
forward the definitions to that to save duplication.


Not with the template as is, since template argument deduction cannot
work on the template as you have currently written it. However, in the
normal case you do this:

In .h:

template class forward declaration

template function (needing friendship) forward declaration

template class definition, including friend (as you wrote it)

In .cpp

Explicit instantion of required versions.

Tom
 
P

porschberg

Thanks a lot for your advice.
I still get a compiler warning

Update.hh:63: warning: friend declaration `otl_stream& operator<<(otl_stream&,
const boost::shared_ptr<typename T::row>&)' declares a non-template function
Update.hh:63: 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

but there is nothing wrong with operator<< because it is not a
template function. Right?!
My real implementation is in *.cc.
 
T

tom_usenet

Thanks a lot for your advice.
I still get a compiler warning

Update.hh:63: warning: friend declaration `otl_stream& operator<<(otl_stream&,
const boost::shared_ptr<typename T::row>&)' declares a non-template function
Update.hh:63: 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

but there is nothing wrong with operator<< because it is not a
template function. Right?!

Right. Normally it is a mistake (you intended a template function),
but in your case it isn't. That looks like a gcc warning. See here:

http://gcc.gnu.org/faq.html#friend

Tom
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top