Connecting Tech Pros Worldwide Help | Site Map

Should this compile and link?

  #1  
Old July 23rd, 2005, 06:50 AM
russ@haydon449.plus.com
Guest
 
Posts: n/a
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::ostream & operator<< ( std::ostream & os, const typename A<T>::B &
b )
{
std::copy( b.innervec.begin(), b.innervec.end(),
std::ostream_iterator<T>( os, "\t" ) );
return os;
}

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

std::copy( a.outervec.begin(), a.outervec.end(),
std::ostream_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?

  #2  
Old July 23rd, 2005, 06:50 AM
Victor Bazarov
Guest
 
Posts: n/a

re: Should this compile and link?


russ@haydon449.plus.com wrote:[color=blue]
> Been looking at some code.....
> [...]
> template < typename T >
> std::ostream & operator<< ( std::ostream & os, const typename A<T>::B[/color]

'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.

[color=blue]
> & b )
> [...]
>
> Is that standard conforming?[/color]

I don't think so.
[color=blue]
> Should it work fine as is?[/color]

I don't think so.
[color=blue]
> 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?[/color]

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


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Should this short (but abstruse) program compile? Howard Gardner answers 6 July 21st, 2006 04:05 PM
Using vcbuild to compile/link files from MSVC.NET 7.1 3doutpost answers 10 November 17th, 2005 03:06 PM
REPOST: extremely long link times Steve McLellan answers 14 November 17th, 2005 03:01 PM
gcc compile / link questions Edward C. Jones answers 1 July 18th, 2005 05:41 AM