Topic: Partial specialization of function template


Author: Gabriel Dos_Reis <gdosreis@sophia.inria.fr>
Date: Wed, 3 Jan 2001 22:44:45 GMT
Raw View
qrczak@knm.org.pl (Marcin 'Qrczak' Kowalczyk) writes:

| Tue, 26 Dec 2000 17:53:28 GMT, Sebastian Moleski <smoleski@surakware.com>=
|  pisze:
|
| > template<class T> T Eng();
| >=20
| > he wanted to create a specialization for Eng's that handle pointers to =
| T.
|
| There are no specializations of function templates.

Function templates can be specialized: Such specializations correspond
to what are named "explicit specializations".

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr

---
[ 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: "Sebastian Moleski" <smoleski@surakware.com>
Date: Tue, 26 Dec 2000 17:53:28 GMT
Raw View
Hi,

someone is currently asking a question in the newsgroups for Borland's C++
Builder's about function templates and specializations. Given the following
function template:

template<class T> T Eng();

he wanted to create a specialization for Eng's that handle pointers to T.
The syntax I came up with was

/* 9 */ template<> template<class T> T End<T *>();

However, the compiler issues these error messages:
[C++ Error] Unit1.cpp(9): E2141 Declaration syntax error
[C++ Error] Unit1.cpp(9): E2451 Undefined symbol 'T'
[C++ Error] Unit1.cpp(9): E2188 Expression syntax
[C++ Error] Unit1.cpp(14): E2428 Templates must be classes or functions
[C++ Error] Unit1.cpp(14): E2238 Multiple declaration for 'Eng<T>()'
[C++ Error] Unit1.cpp(6): E2344 Earlier declaration of 'Eng<T>()'
[C++ Error] Unit1.cpp(14): E2473 Invalid explicit specialization of
'Eng<T>()'
[C++ Error] Unit1.cpp(14): E2141 Declaration syntax error

(BTW, there IS no line 14.)

I had a look at the standard but could not really find a problem with the
above. Indeed, there's even an example using this syntax for a member
template function specialization.

So my question is: is C++ Builder right to reject the syntax above? If so,
what is the correct syntax?

Thanks in advance,

Sebastian Moleski


---
[ 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: "Sebastian Moleski" <smoleski@surakware.com>
Date: Tue, 26 Dec 2000 18:57:31 GMT
Raw View
Hi,

someone is currently asking a question in the newsgroups for Borland's C++
Builder's about function templates and specializations. Given the following
function template:

template<class T> T Eng();

he wanted to create a specialization for Eng's that handle pointers to T.
The syntax I came up with was

/* 9 */ template<> template<class T> T End<T *>();

However, the compiler issues these error messages:
[C++ Error] Unit1.cpp(9): E2141 Declaration syntax error
[C++ Error] Unit1.cpp(9): E2451 Undefined symbol 'T'
[C++ Error] Unit1.cpp(9): E2188 Expression syntax
[C++ Error] Unit1.cpp(14): E2428 Templates must be classes or functions
[C++ Error] Unit1.cpp(14): E2238 Multiple declaration for 'Eng<T>()'
[C++ Error] Unit1.cpp(6): E2344 Earlier declaration of 'Eng<T>()'
[C++ Error] Unit1.cpp(14): E2473 Invalid explicit specialization of
'Eng<T>()'
[C++ Error] Unit1.cpp(14): E2141 Declaration syntax error

(BTW, there IS no line 14.)

I had a look at the standard but could not really find a problem with the
above. Indeed, there's even an example using this syntax for a member
template function specialization.

So my question is: is C++ Builder right to reject the syntax above? If so,
what is the correct syntax?

Thanks in advance,

Sebastian Moleski




---
[ 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: "Andrei Iltchenko" <iltchenko@yahoo.com>
Date: Wed, 27 Dec 2000 10:27:22 GMT
Raw View
> Hi,
>
> someone is currently asking a question in the newsgroups for Borland's C++
> Builder's about function templates and specializations. Given the
following
> function template:
>
> template<class T> T Eng();
>
> he wanted to create a specialization for Eng's that handle pointers to T.
> The syntax I came up with was
>
> /* 9 */ template<> template<class T> T End<T *>();
>
The syntax you came up with is erroneous indeed, as
it is neither an explicit instantiation nor an explicit
specialization for the function template Eng();

If you meant to provide a partial specialization of the
function template Eng for pointers, then the language
has no notion of function template partial specializations,
only class templates can have partial specializations.

If you want to explicitly instantiate the function template Eng for,
say, a pointer to int, you should use something like:
template int* Eng<int*>();

If you want to explicitly specialize the function template Eng for
int*, you ought to use a construct like:
template<> int* Eng<int>()
{
  ...
  ...
}

Andrei Iltchenko
Brainbench MVP for C++
http://www.brainbench.com


---
[ 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: Alexander Nasonov <alnsn@mail.ru>
Date: Wed, 27 Dec 2000 13:01:28 GMT
Raw View
In article <926qpp$juu$02$1@news.t-online.com>,
  "Sebastian Moleski" <smoleski@surakware.com> wrote:
> Hi,
>
> someone is currently asking a question in the newsgroups for
Borland's C++
> Builder's about function templates and specializations. Given the
following
> function template:
>
> template<class T> T Eng();
>
> he wanted to create a specialization for Eng's that handle pointers
to T.
> The syntax I came up with was
>
> /* 9 */ template<> template<class T> T End<T *>();
>
> However, the compiler issues these error messages:
skiped
> I had a look at the standard but could not really find a problem with
the
> above. Indeed, there's even an example using this syntax for a member
> template function specialization.
>
> So my question is: is C++ Builder right to reject the syntax above?
If so,
> what is the correct syntax?

 template<class T> T Eng (); // (1)

Specialization for pointers could look like that:
 template<class T> T *Eng (); // (2)
but versions (1), (2) of Eng <SomeType> only differ by return type.
You can obtain return value of Eng using function with parameter passed
by reference:

 template <typename T> void f (T &);
 template <typename T> void f (T *&);

 int i, *p;
 f <int> (i); // can be modified
 f <int *> (p); // can be modified

Here is the specialization for the void * type:
 template<> void f <void *> (void *);


Sent via Deja.com
http://www.deja.com/

---
[ 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: qrczak@knm.org.pl (Marcin 'Qrczak' Kowalczyk)
Date: Wed, 27 Dec 2000 19:00:05 GMT
Raw View
Tue, 26 Dec 2000 17:53:28 GMT, Sebastian Moleski <smoleski@surakware.com>=
 pisze:

> template<class T> T Eng();
>=20
> he wanted to create a specialization for Eng's that handle pointers to =
T.

There are no specializations of function templates. Only overloading
with ordering rules, which won't work in this case because overloaded
variants must differ in parameters.

You can put definitions in functions taking an additional parameter
used to disambiguate types and wrap them in the interface you wanted:

template <class T> T  Eng_aux (T  *) {general definition;}
template <class T> T *Eng_aux (T **) {specialized definition;}

template <class T> T Eng()
{
    return Eng_aux ((T *) 0);
}

or put the function in a class template and specialize wrt. class
parameters.

> I had a look at the standard but could not really find a problem
> with the above. Indeed, there's even an example using this syntax
> for a member template function specialization.

This is probably specialization wrt. template parameters of the class,
not of the member itself.

--=20
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZAST=CAPCZA
QRCZAK

---
[ 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.     ]