Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Should this compile and link?

Reply
Thread Tools

Should this compile and link?

 
 
russ@haydon449.plus.com
Guest
Posts: n/a
 
      07-04-2005
Been looking at some code.....

#include <vector>
#include <iostream>
#include <algorithm>

template <typename T> struct A
{
struct B
{
std::vector< T > innervec;
} ;

std::vector< B > outervec;
};

template < typename T >
std:stream & operator<< ( std:stream & os, const typename A<T>::B &
b )
{
std::copy( b.innervec.begin(), b.innervec.end(),
std:stream_iterator<T>( os, "\t" ) );
return os;
}

template < typename T >
std:stream & operator<< ( std:stream & os, const A<T>& a )
{
typedef typename A<T>::B B_t;

std::copy( a.outervec.begin(), a.outervec.end(),
std:stream_iterator< B_t >( os, "\n" ) );
return os;
}

int main()
{
A<int> a;
A<int>::B b1;
A<int>::B b2;
b1.innervec.push_back( 11 );
b1.innervec.push_back( 12 );
b2.innervec.push_back( 21 );
b2.innervec.push_back( 22 );
a.outervec.push_back( b1 );
a.outervec.push_back( b2 );
std::cout << a;
}

Is that standard conforming?
Should it work fine as is?
My copy of MSVC 7.0 doesn't complain but Comeau's online beta says no
and fails when -tused is used.
If this is not supposed to work then why is that and what is the best
way of rewriting it?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-05-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Been looking at some code.....
> [...]
> template < typename T >
> std:stream & operator<< ( std:stream & os, const typename A<T>::B


'const typename A<T>::B &' is not one of the forms which the Standard
allows for template argument deduction from a function argument. So,
the compiler simply fails to deduce 'T' here, most likely.


> & b )
> [...]
>
> Is that standard conforming?


I don't think so.

> Should it work fine as is?


I don't think so.

> My copy of MSVC 7.0 doesn't complain but Comeau's online beta says no
> and fails when -tused is used.
> If this is not supposed to work then why is that and what is the best
> way of rewriting it?


Why should 'B' be inner to A<T>? Perhaps you should make 'B' a template
as well? The only work-around I can think of is to make 'B' a class not
nested in 'A<T>' but a separate class.

V


 
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
compile directive for conditional compile for Java 1.4 versus Java 5 timjowers Java 7 02-02-2011 12:08 AM
How to compile the following source code in VC6// I have error inVC++6 but compile ok in GCC fAnSKyer C++ 2 06-07-2009 07:57 AM
computation at compile time i.e. compile time functions usingtemplates Carter C++ 2 03-04-2009 06:43 PM
Compile versus not compile (VS 2005)?? stupid48@gmail.com ASP .Net 1 04-11-2008 08:24 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM



Advertisments