Specializing template constructors

Specializing template constructors

Post by Omer Tunal » Wed, 30 Jul 2003 19:31:47



I have the following class template :

template <class T> struct A {
   template<class U> A(const U&){ cout << __PRETTY_FUNCTION__ << endl; }

Quote:};

How do I specialize member template constructor for A<int> ?

The following does not compile :

template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__ <<
endl; }

gcc 3.2, icc, and como gives similar error messages. The following is
from como :

"constructor_specialization.C", line 9: error: no instance of overloaded
           function "A<T>::A [with T=int]" matches the specified type
   template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__ <<
endl; }
                             ^

Thanks in advance,

Omer Tunali

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by Ben Hutching » Thu, 31 Jul 2003 07:39:43



> I have the following class template :

> template <class T> struct A {
>    template<class U> A(const U&){ cout << __PRETTY_FUNCTION__ << endl; }
> };

> How do I specialize member template constructor for A<int> ?

> The following does not compile :

> template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__ <<
> endl; }

<snip>

This is a specialisation of a member template of a class template, so
you must include two "template" prefixes.  Add "template<>" before
"template<class T>".  (VC++ 7.0 doesn't accept this, but 7.1 probably
does.  Comeau and G++ accept it.)

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by klau » Thu, 31 Jul 2003 07:42:21



Quote:> I have the following class template :

> template <class T> struct A {
>    template<class U> A(const U&){ cout << __PRETTY_FUNCTION__ << endl; }
> };

> How do I specialize member template constructor for A<int> ?

> The following does not compile :

> template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__ <<
> endl; }

> gcc 3.2, icc, and como gives similar error messages. The following is
> from como :

> "constructor_specialization.C", line 9: error: no instance of overloaded
>            function "A<T>::A [with T=int]" matches the specified type
>    template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__ <<
> endl; }
>                              ^

> Thanks in advance,

> Omer Tunali

I don't now if this is what you are after:

template <class T, class U>

class A {

A(const U&)

{

cout << __PRETTY_FUNCTION__ << endl;

Quote:}
};

template <class T>

class A<T, int> {

A(const int&)

{

cout << __PRETTY_FUNCTION__ << endl;

Quote:}
};

If this is what you are after please see:

http://www.codeguru.com/cpp_managed/kmg18.html

Regards

Klaus

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by Richard Smit » Thu, 31 Jul 2003 07:45:42



> I have the following class template :

> template <class T> struct A {
>    template<class U> A(const U&){ cout << __PRETTY_FUNCTION__ << endl; }
> };

> template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__ <<
> endl; }

This should read

  template <>
  template <class T>
  A<int>::A( const T& )
  {  cout << __PRETTY_FUNCTION__ << endl; }

Working through line by line, the first line signifies that
it is an explicit specialisation.  All explicit
specialisations must start with "template <>".  The next
line is because the constructor itself is still templated:
in terms of the primary template, you've fixed T to be int,
but U can still vary.

I find that Vandevoorde & Josuttis book, "C++ Templates: The
Complete Guide" is a very useful reference if you find
yourself writing complex template code (such as this) on a
frequent basis.

--
Richard Smith

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by Sebastian Molesk » Thu, 31 Jul 2003 07:48:55



Quote:> I have the following class template :

> template <class T> struct A {
>    template<class U> A(const U&){ cout << __PRETTY_FUNCTION__ << endl; }
> };

> How do I specialize member template constructor for A<int> ?

> The following does not compile :

> template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__ <<
> endl; }

template<> A<int>::A(const int&) { cout << _PRETTY_FUNCTION__ << endl; }

HTH,

sm

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by llewell » Thu, 31 Jul 2003 08:00:56



> I have the following class template :

> template <class T> struct A {
>    template<class U> A(const U&){ cout << __PRETTY_FUNCTION__ << endl; }
> };

> How do I specialize member template constructor for A<int> ?

> The following does not compile :

> template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__ <<
> endl; }

> gcc 3.2, icc, and como gives similar error messages. The following is
> from como :

> "constructor_specialization.C", line 9: error: no instance of overloaded
>            function "A<T>::A [with T=int]" matches the specified type
>    template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__ <<
> endl; }

[snip]

    template <class T> struct A {
      template<class U> A(const U&);
    };

    template<> template<class U>
      A<int>::A(U const&) { cout << __PRETTY_FUNCTION__ << endl; }

The ' template<> ' prefix is necessar to indicate an explicit
    specialization. I used 'U' instead of 'T' in order to be
    consistent with the member declaration inside the class.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by Jakob Bielin » Thu, 31 Jul 2003 08:14:54



Quote:> I have the following class template :

> template <class T> struct A {
>    template<class U> A(const U&){ cout << __PRETTY_FUNCTION__ << endl; }
> };

> How do I specialize member template constructor for A<int> ?

> The following does not compile :

> template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__ <<
> endl; }

    The following should compile:

template <class T>
struct A
{
    template <class U> A (U const &)
    {
        std::cout << "__PRETTY_FUNCTION__" << std::endl;
    }

    template <>
    A (A <int> const&)
    {
        std::cout << "__PRETTY_FUNCTION__" << std::endl;
    }

Quote:};

hth
--
jb

(replace y with x if you want to reply by e-mail)

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by Michael Smolens » Thu, 31 Jul 2003 17:17:09


 > I have the following class template :
 >
 > template <class T> struct A {
 >    template<class U> A(const U&){ cout << __PRETTY_FUNCTION__ << endl; }
 > };
 >
 > How do I specialize member template constructor for A<int> ?
 >
 > The following does not compile :
 >
 > template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__ <<
 > endl; }
 >

you can do

struct A<int>
{
  template<class U> A(const U&) {cout << __PRETTY_FUNCTION__ << endl; }

Quote:};

-Mike

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by Omer Tuna » Thu, 31 Jul 2003 17:07:46


 > gcc 3.2, icc, and como gives similar error messages. The following is
 > from como :
 >
 > "constructor_specialization.C", line 9: error: no instance of overloaded
 >            function "A<T>::A [with T=int]" matches the specified type
 >    template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__ <<
 > endl; }
 >                              ^

The following compiles but it is not what I want to do, I don't want
to write the whole class for the specialization A<int> :

template <class T> struct A {
   template<class U> A(const U&){ cout << __PRETTY_FUNCTION__ << endl;

Quote:}
};

template <> struct A<int> {
   template<class T> A<int>::A(const T&) {cout << __PRETTY_FUNCTION__
<< endl; }

Quote:};

Why isn't

template<class T> A<int>::A(const T&) { cout << __PRETTY_FUNCTION__ <<
endl; }

matched by member function template

template<class U> A(const U&){ cout << __PRETTY_FUNCTION__ << endl; }

of class template A ?

I am aware of the fact that it is not allowed to specialize the
structure (data members) without specializing the whole template for
the special type (in this case an "int".

I am also aware of the fact that it is not allowed to specialize
member functions for templates with multiple template parameters.

I don't think that the above case falls into one of the categories
above.

Thanks.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by Thomas D. Dea » Fri, 01 Aug 2003 08:45:36


Thanks for the replies.

Omer Tunali provided the clue I needed.  I stripped the class down to a minimum
to demo the question.  However, I added the data member of base class type and
went downhill from there!  This led to adding the two functions to access the
base class find function with the data member.  If I omit the data member and
the two functions, I get what I wanted.  Thinking helps!  Adding the remaining
part of the class and everything sort-of works.  Now, I can return to the
problem at hand.

Thanks,
tomdean

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by Richard Smit » Fri, 01 Aug 2003 08:52:19



> The following should compile:

No it shouldn't.  See below.

Quote:> template <class T>
> struct A
> {
>     template <class U> A (U const &)
>     {
>         std::cout << "__PRETTY_FUNCTION__" << std::endl;
>     }

>     template <>

Explicit specialisations may only be at namespace scope, not
at class scope.  This is illegal.

Furthermore you cannot explicitly specialise member
templates of template classes -- you can only explicitly
specialise member templates of nontemplate classes, such as
explicit specialisations.

Quote:>     A (A <int> const&)

This is not doing what the OP wanted, which was specialise
for the case where T == int and U was anything.  You
appear to be trying to specialise for the case where T is
anything and U == A<int>.

Quote:>     {
>         std::cout << "__PRETTY_FUNCTION__" << std::endl;
>     }
> };

--
Richard Smith

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by Richard Smit » Fri, 01 Aug 2003 08:53:40




> > template <class T> struct A {
> >    template<class U> A(const U&){ cout << __PRETTY_FUNCTION__ << endl; }
> > };

> template<> A<int>::A(const int&) { cout << _PRETTY_FUNCTION__ << endl; }

No.  This is *not* legal.  If you want to specialise the
constructor in the case where T == int and U == int, then
you must write

  template <> template <>
  A<int>::A(const int&) { /* ... */ }

If, as I understood the OP's question, you want to
specialise if from the case where T == int and U is
still free to vary, then you must write

  template <> template <class U>
  A<int>::A(const U&) { /* ... */ }

--
Richard Smith

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by Omer Tuna » Fri, 01 Aug 2003 21:40:32


 >
 > Working through line by line, the first line signifies that
 > it is an explicit specialisation.  All explicit
 > specialisations must start with "template <>".  The next
 > line is because the constructor itself is still templated:
 > in terms of the primary template, you've fixed T to be int,
 > but U can still vary.
 >

Thank you (and other posters) for the detailed answers. It is exactly
what I was looking for. I am not sure if all explicit specializations
must start with "template <>" though. An explicit specialization can
also be a partial specialization, I guess.

 > I find that Vandevoorde & Josuttis book, "C++ Templates: The
 > Complete Guide" is a very useful reference if you find
 > yourself writing complex template code (such as this) on a
 > frequent basis.

I ordered the book. Thank you.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by Francis Glassboro » Sat, 02 Aug 2003 03:00:06




Quote:>Thanks for the replies.

>Omer Tunali provided the clue I needed.  I stripped the class down to a minimum
>to demo the question.  However, I added the data member of base class type and
>went downhill from there!  This led to adding the two functions to access the
>base class find function with the data member.  If I omit the data member and
>the two functions, I get what I wanted.  Thinking helps!  Adding the remaining
>part of the class and everything sort-of works.  Now, I can return to the
>problem at hand.

With the caveat that you are in undefined land because you are deriving
from a class that does not have a virtual dtor. You also have a derived
to base conversion problem waiting to bite when you least expect it.

--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow      ACCU

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Specializing template constructors

Post by Richard Smit » Sat, 02 Aug 2003 09:58:36



> I am not sure if all explicit specializations must start
> with "template <>" though. An explicit specialization can
> also be a partial specialization, I guess.

Pedantically, no, a partial specialisation is never an
explicit specialisation.  They are different (though closely
related) things.  So an explicit specialisation will always
start "template <>" and a partial specialisation will always
start "template </* arguments */>".  In retrospect, it might
have been better if explicit specialisations were called
complete specialisations as this much better describes how
they are related to partial specialisations.

--
Richard Smith

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

1. Simple question (Specialized templates within non-specialized templates)

Hi all you C++ gurus,

Consider this small example:

template<typename T, int I> class A {} ;

class B {};   // no templates
class C {};

 I have an explicitly specialized function that returns different
stuff based on what is the template parameter:

template <typename T> func();
template<> int func<B>();
template<> int func<C>();

Now I want this function to serve the A. BUT

template<typename T, int I>
template<> function<A<T,I> > func() {};

is illegal, because the standard prohibits specialization of a
template within a NON-specialized template.

Any workarounds?

Thanks!!


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. US-NC EXPER. POWERBUILDER DEVELOPER

3. Can you specialize a Template class by specifying only the member function that should be specialized?

4. cannot get the frame cloud up and running

5. Calling default constructor from specialized constructor

6. Recommendation

7. Q: How to provide a partially specialized template inside a template class?

8. Convert a 3x3 vector to a column vector

9. Specializing template class members (not class member templates)

10. Q: Specializing member templates of template classes

11. Specializing member function templates (was Re: Template virtual function?)

12. Q: Specializing templates with complex<T> or other templates

13. Possible to write constructor templates within non-template classes ?