473,569 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator<< for templated subclass?

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>::subra nge' object
into a 'std::basic_ost ream<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::ostream' 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>::subra nge'
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<typena me 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_ostr eam<charT, traits> &operator<<(
std::basic_ostr eam<charT, traits> &os,
matrix<ELEMENT_ T, INDEX_T>::subra nge const &range)
{
os <<"matrix range";
return os;
}

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

sincerly,

Guenther

Jul 19 '05 #1
4 3818
Guenther Brunthaler wrote:
std::basic_ostr eam<charT, traits> &operator<<(
std::basic_ostr eam<charT, traits> &os,
matrix<ELEMENT_ T, INDEX_T>::subra nge const &range)


typename matrix<ELEMENT_ T, INDEX_T>::subra nge const &range)

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

--
Attila aka WW
Jul 19 '05 #2
On Tue, 30 Sep 2003 10:03:42 GMT, Guenther Brunthaler
<no************ ********@yahoo. com> 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>::subra nge' object
into a 'std::basic_ost ream<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::ostrea m' 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>::subra nge 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<ELEMEN T_T, INDEX_T>'
instead of a
'matrix<ELEMEN T_T, INDEX_T>::subra nge'
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<typena me ELEMENT_T, typename INDEX_T>
struct matrix {
// I want to output an object of this nested type.
struct subrange
{
template<typena me charT, typename traits>
friend std::basic_ostr eam<charT, traits> &operator<<(
std::basic_ostr eam<charT, traits> &os,
subrange const &range)
{
os <<"matrix range";
return os;
}
};
};

int main(int, char **) {
matrix<double, size_t>::subran ge 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>::subran ge 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<cha r> >.

Tom
Jul 19 '05 #3
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.)

Jul 19 '05 #4
On Wed, 01 Oct 2003 03:57:08 GMT, Guenther Brunthaler
<no************ ********@yahoo. com> 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
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7134
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): <code:templates.h> #include <string> #include <iostream>
0
484
by: Guenther Brunthaler | last post by:
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:
1
1650
by: Jacob Foshee | last post by:
Greetings, Using MS Visual C++ .NET 2003, I have the following: template <class T> class vector3 { // ... friend ostream& operator<<(ostream&, const vector3<T>& ); // ostream operator }:
1
1494
by: Eddie Parker | last post by:
Hi! I'm having an interesting problem that I can't seem to get to work, and I'm curious if someone could either a) tell me how to make it work, or b) tell me why it *can't* work. :) Anyhow, here's a small test case that should compile on most compilers: #include <iostream>
4
2492
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
4
2640
by: homsan toft | last post by:
I've tried the below code with MSVC and Comeau online compiler. Both complain that operator<< for Outer<part<size_t> >::inner is not defined. So how do I declare it without doing full specialization? I've tried a template on any type, like Outer<AnyT>::inner (see below) Also tried a template on the templated type: Outer<part<X>...
7
14902
by: glen | last post by:
Hi. I'm using GCC 4.1.1, which I mention since I don't know if this is a compiler issue, or me not understanding some subtlety in the standard. The code below compiles fine under vc++, but I'm having trouble using gcc. It's just a templated container output. /** outputs elements of a vector in the form '{el1,el2...elEnd}'
6
1605
by: johnmmcparland | last post by:
Hi all, when I write a subclass, can I inherit its superclass' << operator? As an example say we have two classes, Person and Employee. class Person has the << operator; /**
1
1397
by: Stuart Golodetz | last post by:
Hi guys, I'm trying to making an instance of a templated operator<< for a templated class a friend of that class (see below), to allow it to access the class internals for output purposes. #include <iostream> template <typename T> class TC
4
1879
by: =?ISO-8859-1?Q?Dar=EDo_Griffo?= | last post by:
I'm having an error with this code #include <iostream> template < typename Tclass TestOpTemplate { public: friend std::ostream& operator<< <>(std::ostream& os, const TestOpTemplate<T>& m); };
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6286
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.