Two templates with overlap.

Discussion in 'C++' started by Jason Heyes, Dec 11, 2003.

  1. Jason Heyes

    Jason Heyes Guest

    Consider these two templates:

    template <class T>
    std::istream &operator>>(std::istream &is, T &object);

    template <class T>
    std::istream &operator>>(std::istream &is, SharedPtr<T> &ptr);

    What will the compiler do when it sees the following code?

    Foo foo;
    cin >> foo;

    I presume that the compiler will instantiate the template std::istream
    &operator>>(std::istream &is, T &object) with Foo. How about this code?

    SharedPtr<Bar> bar;
    cin >> bar;

    Now I am not sure of what choice the compiler will make. Will it instantiate
    the first function or the second? Thanks.
     
    Jason Heyes, Dec 11, 2003
    #1
    1. Advertising

  2. Jason Heyes wrote:
    > Consider these two templates:
    >
    > template <class T>
    > std::istream &operator>>(std::istream &is, T &object);
    >
    > template <class T>
    > std::istream &operator>>(std::istream &is, SharedPtr<T> &ptr);
    >
    > What will the compiler do when it sees the following code?
    >
    > Foo foo;
    > cin >> foo;
    >
    > I presume that the compiler will instantiate the template std::istream
    > &operator>>(std::istream &is, T &object) with Foo. How about this code?
    >
    > SharedPtr<Bar> bar;
    > cin >> bar;
    >
    > Now I am not sure of what choice the compiler will make. Will it instantiate
    > the first function or the second? Thanks.
    >
    >


    It should instantiate the second. If you're using namespaces you may
    need to have a "using namesp::eek:perator>> blah".
     
    Gianni Mariani, Dec 11, 2003
    #2
    1. Advertising

  3. Jason Heyes

    Chris Theis Guest

    "Jason Heyes" <> wrote in message
    news:3fd88945$0$1019$...
    > Consider these two templates:
    >
    > template <class T>
    > std::istream &operator>>(std::istream &is, T &object);
    >
    > template <class T>
    > std::istream &operator>>(std::istream &is, SharedPtr<T> &ptr);
    >
    > What will the compiler do when it sees the following code?
    >
    > Foo foo;
    > cin >> foo;
    >
    > I presume that the compiler will instantiate the template std::istream
    > &operator>>(std::istream &is, T &object) with Foo. How about this code?
    >
    > SharedPtr<Bar> bar;
    > cin >> bar;
    >
    > Now I am not sure of what choice the compiler will make. Will it

    instantiate
    > the first function or the second? Thanks.
    >


    IMHO it should instantiate the second one. Did your compiler do it
    differently?

    Chris
     
    Chris Theis, Dec 11, 2003
    #3
  4. Jason Heyes

    Jason Heyes Guest

    "Chris Theis" <> wrote in message
    news:braae8$e8o$...
    > IMHO it should instantiate the second one. Did your compiler do it
    > differently?


    I still haven't tested it. Let me ask you. Do you think it is a good idea to
    use these two templates?

    template <class T>
    std::istream &operator>>(std::istream &is, T &object)
    { return object.extract(is); }

    template <class T>
    std::eek:stream &operator<<(std::eek:stream &os, const T &object)
    { return object.insert(os); }

    template <class T>
    std::istream &operator>>(std::istream &is, SharedPtr<T> &ptr)
    {
    if (ptr)
    {
    ptr.make_unique();
    return is >> *ptr;
    }

    T object;
    if (!(is >> object))
    return is;
    ptr = new T(object);

    return is;
    }

    template <class T>
    std::eek:stream &operator<<(std::eek:stream &os, const SharedPtr<T> &ptr)
    { return os << *ptr; }


    Thanks.
     
    Jason Heyes, Dec 12, 2003
    #4
  5. Jason Heyes

    Chris Theis Guest

    "Jason Heyes" <> wrote in message
    news:3fd90fa4$0$1012$...
    > "Chris Theis" <> wrote in message
    > news:braae8$e8o$...
    > > IMHO it should instantiate the second one. Did your compiler do it
    > > differently?

    >
    > I still haven't tested it. Let me ask you. Do you think it is a good idea

    to
    > use these two templates?
    >

    [SNIP]

    Whether it's a good idea or not actually depends on your problem and the
    context but from a quick glance at the code I'd say it should be okay.

    Chris
     
    Chris Theis, Dec 12, 2003
    #5
  6. Jason Heyes

    tom_usenet Guest

    On Fri, 12 Dec 2003 11:45:22 +1100, "Jason Heyes"
    <> wrote:

    >"Chris Theis" <> wrote in message
    >news:braae8$e8o$...
    >> IMHO it should instantiate the second one. Did your compiler do it
    >> differently?

    >
    >I still haven't tested it. Let me ask you. Do you think it is a good idea to
    >use these two templates?


    Yes, it's fine, as long as your compiler is up to date (and therefore
    supports partial ordering of function templates).

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Dec 12, 2003
    #6
    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. =?Utf-8?B?SklNLkgu?=

    components overlap

    =?Utf-8?B?SklNLkgu?=, Jul 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    348
    =?Utf-8?B?SklNLkgu?=
    Jul 29, 2004
  2. =?Utf-8?B?V1RT?=

    combo box overlap pull down menu

    =?Utf-8?B?V1RT?=, Dec 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    639
    Jason
    Dec 3, 2004
  3. JKop
    Replies:
    3
    Views:
    505
  4. Robin Haswell

    Determing whether two ranges overlap

    Robin Haswell, Feb 16, 2006, in forum: Python
    Replies:
    6
    Views:
    3,024
    Magnus Lycka
    Feb 22, 2006
  5. recover
    Replies:
    2
    Views:
    845
    recover
    Jul 25, 2006
Loading...

Share This Page