Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > operator<< for templated subclass?

Reply
Thread Tools

operator<< for templated subclass?

 
 
Guenther Brunthaler
Guest
Posts: n/a
 
      09-30-2003
Hi template specialists,

I have a problem with the code listed below.

What I wanted to do is defining an operator<< that is able to output
a 'matrix<ELEMENT_T, INDEX_T>::subrange' object
into a 'std::basic_ostream<charT, traits>'.

For some reason, it does not work. The compiler always complains:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
te.cpp:
Error E2094 test.cpp: 'operator<<' not implemented in type
'std:stream' for arguments of type 'matrix<double,unsigned
int>::subrange' in function main(int,char * *)
*** 1 errors in Compile ***

The INTEL compiler gives a similar error.

On the other hand, MSVC 7 compiles without any warnings.

So - which of the compilers are correct?

And does anyone have an idea what there might be wrong with the code?

Note:

If I change the code to output a
'matrix<ELEMENT_T, INDEX_T>'
instead of a
'matrix<ELEMENT_T, INDEX_T>::subrange'
then it works perfectly for all compilers!

Only that this is not what I actually want to do.

So, please help me if you possibly can!

---------snip-----------
#include <iostream>
#include <cstdlib>


template<typename ELEMENT_T, typename INDEX_T>
struct matrix {
// I want to output an object of this nested type.
struct subrange {};
};

// This templated global function will not be used
// due to some reason unknown to me.
template<
typename ELEMENT_T, typename INDEX_T, typename charT, typename traits
>

std::basic_ostream<charT, traits> &operator<<(
std::basic_ostream<charT, traits> &os,
matrix<ELEMENT_T, INDEX_T>::subrange const &range)
{
os <<"matrix range";
return os;
}

int main(int, char **) {
matrix<double, size_t>::subrange x;
std::cout << x;
return EXIT_SUCCESS;
}
---------snip-----------

sincerly,

Guenther



 
Reply With Quote
 
 
 
 
Attila Feher
Guest
Posts: n/a
 
      09-30-2003
Guenther Brunthaler wrote:
> std::basic_ostream<charT, traits> &operator<<(
> std::basic_ostream<charT, traits> &os,
> matrix<ELEMENT_T, INDEX_T>::subrange const &range)


typename matrix<ELEMENT_T, INDEX_T>::subrange const &range)

http://www.cuj.com/documents/s=8464/cuj0308dewhurst/

--
Attila aka WW


 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      09-30-2003
On Tue, 30 Sep 2003 10:03:42 GMT, Guenther Brunthaler
<> wrote:

>Hi template specialists,
>
>I have a problem with the code listed below.
>
>What I wanted to do is defining an operator<< that is able to output
>a 'matrix<ELEMENT_T, INDEX_T>::subrange' object
>into a 'std::basic_ostream<charT, traits>'.
>
>For some reason, it does not work. The compiler always complains:
>
>Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
>te.cpp:
>Error E2094 test.cpp: 'operator<<' not implemented in type
>'std:stream' for arguments of type 'matrix<double,unsigned
>int>::subrange' in function main(int,char * *)
>*** 1 errors in Compile ***
>
>The INTEL compiler gives a similar error.
>
>On the other hand, MSVC 7 compiles without any warnings.
>
>So - which of the compilers are correct?


INTEL and Borland. matrix<ELEMENT_T, INDEX_T>::subrange is a
nondeduced context since it requires template argument deduction of
the template parameters of a parent type based on the type of a nested
type. The compiler can't perform this deduction in general, because
the mapping of subtype onto template parameter types is not in general
many to one, but many to many.

>And does anyone have an idea what there might be wrong with the code?
>
>Note:
>
>If I change the code to output a
>'matrix<ELEMENT_T, INDEX_T>'
>instead of a
>'matrix<ELEMENT_T, INDEX_T>::subrange'
>then it works perfectly for all compilers!


Right, since that doesn't require argument deduction from a nested
type.

>Only that this is not what I actually want to do.
>
>So, please help me if you possibly can!


This version works:

#include <iostream>
#include <cstdlib>

template<typename ELEMENT_T, typename INDEX_T>
struct matrix {
// I want to output an object of this nested type.
struct subrange
{
template<typename charT, typename traits>
friend std::basic_ostream<charT, traits> &operator<<(
std::basic_ostream<charT, traits> &os,
subrange const &range)
{
os <<"matrix range";
return os;
}
};
};

int main(int, char **) {
matrix<double, size_t>::subrange x;
std::cout << x;
return EXIT_SUCCESS;
}

http://www.parashift.com/c++-faq-lit...html#faq-34.15
is the inverse of what I've done above - the linker errors have been
avoided by putting the definition inline. The template argument
deduction problem has been avoided by making removing the ELEMENT_T
and INDEX_T template parameters from operator<< so that it doesn't
have to deduce them.

When matrix<double, size_t>::subrange is instantiated, the friend
function will also be injected into the global namespace, hence koenig
lookup on the operator<< call finds the template and instantiates and
calls it for <char, char_traits<char> >.

Tom
 
Reply With Quote
 
Guenther Brunthaler
Guest
Posts: n/a
 
      10-01-2003
Thanks a lot, Tom and Attila!

tom_usenet wrote:

> This version works:


But unfortunately not for C++ Builder. Nor for MSVC. Nor GCC.

But at least it works for INTEL.

Nevertheless, thanks to you now I know at least what the problem was!

The fact that so many C++ Compiler still do not comply with the C++
standard is a different story anyway


sincerly,

Guenther

(Note: If you want to reply via e-mail, please remove the
substrings "nospam_" and "_dontspam" from my address.)

 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      10-01-2003
On Wed, 01 Oct 2003 03:57:08 GMT, Guenther Brunthaler
<> wrote:

>Thanks a lot, Tom and Attila!
>
>tom_usenet wrote:
>
>> This version works:

>
>But unfortunately not for C++ Builder. Nor for MSVC. Nor GCC.
>
>But at least it works for INTEL.
>
>Nevertheless, thanks to you now I know at least what the problem was!
>
>The fact that so many C++ Compiler still do not comply with the C++
>standard is a different story anyway


GCC 3.2 is fine with it, as is MSVC7.1 (2003). Time to upgrade!

Tom
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling templated member of templated object david@sunlightd.com C++ 1 06-22-2007 05:13 PM
templated function as parameter of another templated function Amadeus W. M. C++ 2 07-04-2006 09:59 PM
ASP.NET Templated User Controls - Limit child controls allowable within a templated control JohnyStyles@gmail.com ASP .Net 0 05-29-2006 06:00 PM
Subtypes of templated types (in templated functions) Marijn C++ 5 02-13-2004 09:50 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57