Operator<<, in nested templated classes.

Discussion in 'C++' started by Eddie Parker, Sep 9, 2005.

  1. Eddie Parker

    Eddie Parker Guest

    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>

    using namespace std;

    template<typename T>
    class TemplatedOuter
    {
    public:
    class NestedInner
    {
    public:
    int a;

    NestedInner() : a(0) {};
    };


    TemplatedOuter() : m_NestedInner() {};

    NestedInner m_NestedInner;
    };

    /**
    * This does not.
    */
    template<typename T>
    ostream operator<<(ostream &os, typename TemplatedOuter<T>::NestedInner
    const &nestedInner)
    {
    }

    /**
    * This works...
    */
    template<typename T>
    ostream operator<<(ostream &os, TemplatedOuter<T> const
    &templatedOuter)
    {
    }

    int main(void)
    {
    TemplatedOuter<int> a;

    // This works.
    cout << a;

    // This does not.
    cout << a.m_NestedInner;
    }


    /**

    Output:

    main.cpp: In function `int main()':
    main.cpp:39: error: no match for 'operator<<' in 'std::cout <<
    a.TemplatedOuter<int>::m_NestedInner'

    */

    As you can see, I'm trying to basically have a nested public class, and
    print out the contents of it... But it can't seem to find it... Any
    suggestions on what I'm doing wrong, would be appreciated!

    Thanks!
     
    Eddie Parker, Sep 9, 2005
    #1
    1. Advertising

  2. Eddie Parker wrote:
    > 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. :)


    You're running into "non-deducible context". It's not possible to deduce
    the template argument from a template nested class. What's happening is
    this: you give 'a.m_NestedInner' from which 'T' is supposed to be deduced.
    The list of deducible contexts does *not* include template-name<T>::type.

    > Anyhow, here's a small test case that should compile on most compilers:
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > template<typename T>
    > class TemplatedOuter
    > {
    > public:
    > class NestedInner
    > {
    > public:
    > int a;
    >
    > NestedInner() : a(0) {};


    Drop the traling semicolon from the line above.

    > };
    >
    >
    > TemplatedOuter() : m_NestedInner() {};


    Drop the traling semicolon from the line above.

    >
    > NestedInner m_NestedInner;
    > };
    >
    > /**
    > * This does not.
    > */
    > template<typename T>
    > ostream operator<<(ostream &os, typename TemplatedOuter<T>::NestedInner
    > const &nestedInner)


    template<typename T>
    ostream& ...

    > {


    return os;

    > }
    >
    > /**
    > * This works...
    > */
    > template<typename T>
    > ostream operator<<(ostream &os, TemplatedOuter<T> const


    template<typename T>
    ostream& ...

    > &templatedOuter)
    > {


    return os;

    > }
    >
    > int main(void)
    > {
    > TemplatedOuter<int> a;
    >
    > // This works.
    > cout << a;
    >
    > // This does not.
    > cout << a.m_NestedInner;
    > }
    >
    >
    > /**
    >
    > Output:
    >
    > main.cpp: In function `int main()':
    > main.cpp:39: error: no match for 'operator<<' in 'std::cout <<
    > a.TemplatedOuter<int>::m_NestedInner'
    >
    > */
    >
    > As you can see, I'm trying to basically have a nested public class, and
    > print out the contents of it... But it can't seem to find it... Any
    > suggestions on what I'm doing wrong, would be appreciated!


    The only work-around I know is to specify the template argument explicitly
    in a call:

    ::eek:perator << <int> (cout, a.m_NestedInner);

    Another work-around, of course, is to add a member function 'print' to the
    'NestedInner' class and use it as

    a.m_NestedInner.print(cout);

    V
     
    Victor Bazarov, Sep 9, 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. RA Scheltema
    Replies:
    3
    Views:
    406
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    468
    Marijn
    Feb 13, 2004
  3. Replies:
    0
    Views:
    2,238
  4. Amadeus W. M.
    Replies:
    2
    Views:
    396
    Amadeus W. M.
    Jul 4, 2006
  5. Replies:
    3
    Views:
    289
Loading...

Share This Page