Should this compile and link?

Discussion in 'C++' started by russ@haydon449.plus.com, Jul 4, 2005.

  1. Guest

    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::eek:stream & operator<< ( std::eek:stream & os, const typename A<T>::B &
    b )
    {
    std::copy( b.innervec.begin(), b.innervec.end(),
    std::eek:stream_iterator<T>( os, "\t" ) );
    return os;
    }

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

    std::copy( a.outervec.begin(), a.outervec.end(),
    std::eek: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?
    , Jul 4, 2005
    #1
    1. Advertising

  2. wrote:
    > Been looking at some code.....
    > [...]
    > template < typename T >
    > std::eek:stream & operator<< ( std::eek: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
    Victor Bazarov, Jul 5, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    792
    Kevin Spencer
    May 17, 2005
  2. Nagaraj
    Replies:
    1
    Views:
    832
    Lionel B
    Mar 1, 2007
  3. Josef 'Jupp' SCHUGT

    What the FAQs should and should not contain

    Josef 'Jupp' SCHUGT, Aug 19, 2005, in forum: Ruby
    Replies:
    0
    Views:
    176
    Josef 'Jupp' SCHUGT
    Aug 19, 2005
  4. Andrew
    Replies:
    2
    Views:
    116
    Andrew
    Feb 18, 2009
  5. Jake Barnes
    Replies:
    1
    Views:
    157
Loading...

Share This Page