Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

partial specialization of no arg function

2 views
Skip to first unread message

Pete Seymour

unread,
Sep 4, 2002, 3:28:42 AM9/4/02
to
The following failed to compile with an error message along the lines
of missing parameters

template<typename T> void foo();
template<typename T> void foo<T*>();

and in main()

foo<int>();
foo<int*>();

Where have I gone wrong?

thanks
Peter

Andrey Tarasevich

unread,
Sep 4, 2002, 3:36:31 AM9/4/02
to
Pete Seymour wrote:
> ...
> ...

There's no such thing in C++ as partial specialization of template
functions. Partial specialization is only applicable to class templates. Try
to make do with overloading and/or explicit specialization.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Pete Seymour

unread,
Sep 4, 2002, 10:31:43 AM9/4/02
to
Andrey Tarasevich <andreyta...@hotmail.com> wrote in message news:<3D75B7FF...@hotmail.com>...

> Pete Seymour wrote:
> > ...
> > The following failed to compile with an error message along the lines
> > of missing parameters
> >
> > template<typename T> void foo();
> > template<typename T> void foo<T*>();
> >
> > and in main()
> >
> > foo<int>();
> > foo<int*>();
> >
> > Where have I gone wrong?
> > ...
>
> There's no such thing in C++ as partial specialization of template
> functions. Partial specialization is only applicable to class templates. Try
> to make do with overloading and/or explicit specialization.

I thought that might be the case and admittedly I have no great
application for such functions? In the past I've used overloading

foo(T())

as in std::distance but why is this not allowed. It could prevent
duplication of code such as,

int read_int();
int* read_int_ptr();

etc

Victor Bazarov

unread,
Sep 4, 2002, 11:04:49 AM9/4/02
to
"Pete Seymour" <pe...@seymour17.freeserve.co.uk> wrote...


Partial specialisation of functions can be emulated with
classes.

template<class T> struct readT
{
T storage; // or other implementation
readT() { cin >> storage; }
operator T() { return storage; }
};

template<class T> struct readT<T*>
{
T* pointer;
readT() { pointer = new T; cin >> *pointer; }
operator T*() { return pointer; }
};

int main()
{
int i = readT<int>();
double *pd = readT<double*>();
delete pd;

return i;
}

Victor
--
Please remove capital A's from my address when replying by mail


Shane Neph

unread,
Sep 4, 2002, 6:05:33 PM9/4/02
to
Hi Pete,
'Modern C++ Design' by Andrei Alexandrescu (Ch 2) shows an elegant way to
help deal with the fact that there is no partial specialization for
functions in C++. His method is still, of course, still just function
overloading, but it looks a lot like part. temp. spec.
Here is an example based upon his implementation:

namespace Private { // Implementation
template <typename T>
struct Type2Type
{
typedef T OriginalType;
protected:
~Type2Type() {}
};

template <typename T> void foo( Type2Type<T> )
{ /* your implementation for type T */ }

template <typename T> void foo( Type2Type<T*> )
{ /* your implementation for type T pointer */ }

} // end namespace Private

// This is the only one seen by your program
template <typename T> void foo( )
{
return( Private::foo( Private::Type2Type<T>( ) ) );
}

Now you have a single interface and multiple implementations based upon the
underlying type T. You can even differentiate between const T's and T's.
There is a lot of power in using the class Type2Type.

Hope this is useful - I use it often.

--Regard,
Shane

"Pete Seymour" <pe...@seymour17.freeserve.co.uk> wrote in message
news:917937f7.02090...@posting.google.com...

Shane Neph

unread,
Sep 5, 2002, 12:19:37 AM9/5/02
to
Please ignore what I have said for at least the last 24 hours. I'm
brain-dead and saying some pretty dumb things - I need some Zzzz's.
Andrei's book is awesome, but what I have written below is flat out wrong.
Sorry.
-Shane


"Shane Neph" <eucli...@hotmail.com> wrote in message
news:Nsvd9.8759$JJ1....@nwrddc02.gnilink.net...

0 new messages