repost: template function specialization & basic question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • NKOBAYE027

    repost: template function specialization & basic question...

    FIRST POST

    Hi All: I'm trying to write a simple specialization before moving on to
    something a bit more complex - always a good idea in my case, at least. :o)

    I'm trying to adapt the example from Stroustrup, 3rd ed., The C++
    Programming Language p. 344

    I'm using MSDev 6.0 in case that's an issue. Here's the source...

    /* begin snippet */
    /* specializations .hpp */

    #include <functional>
    using namespace std;

    class foo
    {
    public:
    int m_foo;
    foo():m_foo(0){ }
    };

    template<> bool less<foo>(const foo& afoo, const foo& bfoo)
    {
    return afoo.m_foo < bfoo.m_foo;
    }

    /* specializations test.cpp */
    #include "specialization s.hpp"

    int main()
    {
    foo f,g;
    g.m_foo = 8;

    bool done = less<foo>(f,g);

    return done;
    }

    Compiling...
    specializations test.cpp

    error C2935: 'less<class foo>' : template-class-id redefined as a global
    function

    error C2912: explicit specialization; 'bool __cdecl less<foo>(const class
    foo &,const class foo &)' is not a function template
    see declaration of 'less<class foo>'

    error C2661: 'less<class foo>::less<clas s foo>' : no overloaded function
    takes 2 parameters
    Error executing cl.exe.

    specializations .exe - 3 error(s), 0 warning(s)


    /* end snippet */
    Any help would be appreciated.

    I'm trying to do this with the intent of specializing an iterator operator*
    and & in the stuff I'm working on right now.

    i.e.
    something like

    modified by OP:
    had been glibly using foo as the template args for the lists
    in the example code below - was 3 am, gimme a break :o)
    this makes more sense

    template<typena me T>
    class foo<T>
    {
    public:
    list<T> foolist;
    list<T*>p_fooli st;
    list<int> somestupidlist;
    typedef list<T*>::itera tor iterator;
    }

    template<> T& foo::iterator:: operator*(){/*...*/}

    where dereferencing the iterator will return an object reference, not a
    pointer to the object which is what the list contains.
    so, any suggestions here would be welcome as well...

    And thanks to all those who replied to my prior questions...

    regards,
    L.

    SECOND POST

    Hi folks: Can anyone tell me what the difference is between the two
    functions below? In particular why does the function that returns a
    reference give me warnings about returning the address of a temporary
    variable, whereas the prior function compiles without any errors or warnings

    template<typena me foo>
    class fooset<foo>
    {
    ...
    list<foo> fooList;
    typedef list<foo>::iter ator iterator;
    ...

    public:

    iterator begin()
    {
    return fooList.begin() ;
    }

    /*
    iterator& begin()
    {
    return fooList.begin() ;
    }
    */
    iterator
    };

    thanks,
    and regards,
    L.


  • bartek

    #2
    Re: repost: template function specialization &amp; basic question...

    "NKOBAYE027 " <NKOBAYE027@Rog ers.Com> wrote in
    news:Y9t_b.1206 16$02u1.17142@t wister01.bloor. is.net.cable.ro gers.com:
    [color=blue]
    > Hi All: I'm trying to write a simple specialization before moving on
    > to something a bit more complex - always a good idea in my case, at
    > least. :o)
    >
    > I'm trying to adapt the example from Stroustrup, 3rd ed., The C++
    > Programming Language p. 344
    >
    > I'm using MSDev 6.0 in case that's an issue. Here's the source...
    >[/color]

    (...)
    [color=blue]
    > template<> bool less<foo>(const foo& afoo, const foo& bfoo)
    > {
    > return afoo.m_foo < bfoo.m_foo;
    > }[/color]

    1. std::less is not a function, but a functor. It provides an overloaded
    operator(), so it can be 'invoked' like a function.

    2. Function templates cannot be specialized.

    3. Why don't you write an operator < overload for your foo class?

    Cheers,
    bartek

    Comment

    • Adam H. Peterson

      #3
      Re: repost: template function specialization &amp; basic question...

      > 1. std::less is not a function, but a functor. It provides an overloaded[color=blue]
      > operator(), so it can be 'invoked' like a function.
      >
      > 2. Function templates cannot be specialized.[/color]

      Function templates can be specialized. They cannot be
      _partially_spec ialized_. (OTOH, MSVC6 doesn't support partial template
      specialization anyway.)
      [color=blue]
      >
      > 3. Why don't you write an operator < overload for your foo class?
      >
      > Cheers,
      > bartek[/color]

      Comment

      • bartek

        #4
        Re: repost: template function specialization &amp; basic question...

        "Adam H. Peterson" <ahp6@email.byu .edu> wrote in
        news:c1dpkg$57f l$1@acs2.byu.ed u:
        [color=blue]
        > Function templates can be specialized. They cannot be
        > _partially_spec ialized_. (OTOH, MSVC6 doesn't support partial
        > template specialization anyway.)[/color]

        Oops, of course right. Sorry for the confusion.

        Cheers,
        bartek

        Comment

        • NKOBAYE027

          #5
          Re: repost: template function specialization &amp; basic question...

          Thanks bartek.

          The reason I don't overload the < is because I'm not aiming to do that. This
          was a first attempt at specializing a function - and I failed miserably. :o)
          I see now that I was missing the original definition of the template
          function 'less' provided in the text (on the same page, no less...npi). So,
          I guess, what I should do is simply ask if specializing a list<class
          T*>::iterator is possible such that dereferencing the iterator will return
          the object pointed to by T* instead of T*. If so how?

          Sorry for the confusion - it's all mine!

          I've since tried the specialized version and it works when I appropriately
          define it and the original, but would still greatly appreciate any input on
          the iterator issue mentioned above.

          regards,
          L.


          Comment

          • Adam H. Peterson

            #6
            Re: repost: template function specialization &amp; basic question...

            NKOBAYE027 wrote:[color=blue]
            > Thanks bartek.
            >
            > The reason I don't overload the < is because I'm not aiming to do that. This
            > was a first attempt at specializing a function - and I failed miserably. :o)
            > I see now that I was missing the original definition of the template
            > function 'less' provided in the text (on the same page, no less...npi). So,
            > I guess, what I should do is simply ask if specializing a list<class
            > T*>::iterator is possible such that dereferencing the iterator will return
            > the object pointed to by T* instead of T*. If so how?[/color]

            I don't think you can do this with template specialization. In my
            experience (and I think in all cases) when you specialize a function or
            class, you can't alter the signatures of the functions -- what
            parameters they take, their constness, their return type.

            It looks like you're trying to create something akin to a polymorphic
            container. You can probably search on that -- there's been lots of
            discussion, research, etc. on that topic.

            Personally, I would start to solve such a problem by using composition
            with a standard list<>. Something like:


            #include <list>

            template<typena me T>
            class ptr_list {
            public:
            class iterator;
            // Yada yada
            private:
            std::list<T*> data;
            };

            template<typena me T>
            class ptr_list<T>::it erator {
            public:
            T &operator*() const {return **it;}
            // Yada yada
            private:
            std::list<T*>:: iterator it;
            };


            It would take some work to get the full iterator interface working, but
            you may not need all of that depending on your usage.

            Adam H. Peterson

            Comment

            • NKOBAYE027

              #7
              Re: repost: template function specialization &amp; basic question...

              Thanks, Adam - that was exactly what I was looking for. Your pointer about
              specializations not being able to change function signatures is what I
              needed. So, I do need to reinvent the wheel, so-to-speak, for the iterator
              class. I can see no other way than the one you mentioned - and I'd already
              considered that but was hoping for an easier way out...Ugh...c'e st la vie,
              non?

              regards,
              L.

              "Adam H. Peterson" <ahp6@email.byu .edu> wrote in message
              news:c1dsjo$5gi 8$1@acs2.byu.ed u...[color=blue]
              > NKOBAYE027 wrote:[color=green]
              > > Thanks bartek.
              > >
              > > The reason I don't overload the < is because I'm not aiming to do that.[/color][/color]
              This[color=blue][color=green]
              > > was a first attempt at specializing a function - and I failed miserably.[/color][/color]
              :o)[color=blue][color=green]
              > > I see now that I was missing the original definition of the template
              > > function 'less' provided in the text (on the same page, no less...npi).[/color][/color]
              So,[color=blue][color=green]
              > > I guess, what I should do is simply ask if specializing a list<class
              > > T*>::iterator is possible such that dereferencing the iterator will[/color][/color]
              return[color=blue][color=green]
              > > the object pointed to by T* instead of T*. If so how?[/color]
              >
              > I don't think you can do this with template specialization. In my
              > experience (and I think in all cases) when you specialize a function or
              > class, you can't alter the signatures of the functions -- what
              > parameters they take, their constness, their return type.
              >
              > It looks like you're trying to create something akin to a polymorphic
              > container. You can probably search on that -- there's been lots of
              > discussion, research, etc. on that topic.
              >
              > Personally, I would start to solve such a problem by using composition
              > with a standard list<>. Something like:
              >
              >
              > #include <list>
              >
              > template<typena me T>
              > class ptr_list {
              > public:
              > class iterator;
              > // Yada yada
              > private:
              > std::list<T*> data;
              > };
              >
              > template<typena me T>
              > class ptr_list<T>::it erator {
              > public:
              > T &operator*() const {return **it;}
              > // Yada yada
              > private:
              > std::list<T*>:: iterator it;
              > };
              >
              >
              > It would take some work to get the full iterator interface working, but
              > you may not need all of that depending on your usage.
              >
              > Adam H. Peterson[/color]


              Comment

              Working...