Topic: _partial_ specialization of member functions


Author: Martin Sebor <sebor@roguewave.com>
Date: 07 May 01 17:07:28 GMT
Raw View
Paul Mensonides wrote:
>
> You can get around it like this:
>
> template<class T> class BS {
>     public:
>     static void Function(void) { ... }
> };
>
> template<class T> void BS<T*>::Function(void) { ... }

Huh? This isn't legal. There's no such thing as partial specialization
of member functions of class templates (or even function templates).
Only class templates can be partially specialized.

What you can do is partially specialize a member class template to
indirectly achieve the same effect:

template<class T>
struct A
{
   int foo () { return B<T>::foo (*this); }

   template <class U>
   struct B {
       static int foo (A<T>) { return 0; }
   };

   template <class U>
   struct B<U*> {
       static int foo (A<U*>) { return 1; }
   };
};

Regards
Martin

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]




Author: "Paul Mensonides" <pmenso57@home.com>
Date: 5 May 2001 19:02:51 -0400
Raw View
You can get around it like this:

template<class T> class BS {
    public:
    static void Function(void) { ... }
};

template<class T> void BS<T*>::Function(void) { ... }

You never instantiate these 'classes,' so they don't cost anything.
This is one of the annoying limitations of the current template specifications.

Paul Mensonides

"Emery D. Conrad" <econrad@sarnoff.com> wrote in message
news:3AECC4AA.FC7D7754@sarnoff.com...
: hey all,
:
: sorry for the cross posting, but i made a related post in
: comp.lang.c++.moderated, and i wanted to share this there as well.
[snip]
: --
: Emery Conrad
: Sarnoff Corp.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Carl Daniel" <carl@pixami.com>
Date: 5 May 2001 20:42:52 -0400
Raw View
"Emery D. Conrad" <econrad@sarnoff.com> wrote in message
news:3AECC4AA.FC7D7754@sarnoff.com...
> hey all,
>
> sorry for the cross posting, but i made a related post in
> comp.lang.c++.moderated, and i wanted to share this there as well.
>
> i've been enjoying my read of "modern c++ design" and like any good
> book, it's causing me to realize the (possible) partial instantiation of
> the standard in my head. alexandrescu points out that "although you can
> totally specialize member functions of a class template, you cannot
> partially specialize" them [p27]. is this true? i've read through the
> (what i think are the) relevant parts of the spec. 14.7.3 seems to imply
> that this should be allowed, as it defines a "member function of a class
> template" as an okay explicit specialization (though the word "partial"
> doesn't show up, and the examples are all single parameter class
> templates => complete specialization). it seems to me that
>

14.5.4 (3) states "Each class template patrial specialization is a distinct
template and defiinitions shall be provided for the members of a template
partial specialization".  14.5.4.3 (1) goes on to state "...The members of a
class template partial specialization are unrealted to the members of the
primary template.  Class template partial specialization members that are
used in a way that requires a definition shall be defined; the definition of
the members of the primary template are never used as definitions for
members of a class template partial specialization..."

As to the WHY, maybe one or more of the C++ luminaries might want to
weigh-in...
-cd
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Emery D. Conrad" <econrad@sarnoff.com>
Date: 30 Apr 01 18:49:40 GMT
Raw View
hey all,

sorry for the cross posting, but i made a related post in
comp.lang.c++.moderated, and i wanted to share this there as well.

i've been enjoying my read of "modern c++ design" and like any good
book, it's causing me to realize the (possible) partial instantiation of
the standard in my head. alexandrescu points out that "although you can
totally specialize member functions of a class template, you cannot
partially specialize" them [p27]. is this true? i've read through the
(what i think are the) relevant parts of the spec. 14.7.3 seems to imply
that this should be allowed, as it defines a "member function of a class
template" as an okay explicit specialization (though the word "partial"
doesn't show up, and the examples are all single parameter class
templates => complete specialization). it seems to me that

template <typename A, typename B>
class Foo {
public:
    // ...
    bool Bar(void);
    //...
};

template <> template<typename A>
bool Foo<A, int>::Bar(void) { return true; } // danger, will robinson,
impending compiler spew

template <typename A, typename B>
bool Foo<A, B>::Bar(void) { return false; }

should be reasonble code (maybe not for the compiler guys, but that
never stopped anyone...) since complete specializations like this are
okay:

template <>
bool Foo<double, int>::Bar(void) { return false; }

if this is disallowed by the standard, why (and can you point me to the
clause that does)? if so, is it just the difficulty in implementing the
compiler?

there are, of course, are a number of ways for an application programmer
to circumvent this restriction/deficiency: for example, we could make
Foo<A, B>::Bar virtual and then redefine it in a wrapper class as such:

// wrapper instead of partial spec.
template <typename A>
class FooInt : public Foo<A, int> {
    bool Bar(void) { return true; }
};

naturally, this rubs makes me hackles rise! several specializations
later (FooInt, FooDouble, FooFighters... etc), i'm asking myself why
what "generic programming" really means! :-)  other solutions include
breaking the relevant peices into an interface and doing some magic or
using RTTI in Foo<A,B>::Bar (unthinkable in many designs), neither of
which gives me the warm fuzzies as a general solution.

any comforting words?

emery

--
Emery Conrad
Sarnoff Corp.




      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]