Topic: Can "export" be used with explicit specialization?


Author: Hubert.Holin@lmd.polytechnique.fr (Hubert Holin)
Date: Fri, 21 Oct 2005 14:47:42 GMT
Raw View
Somewhere in the E.U., le 21/10/2005

Bonjour

Reading 14.7.3, it is not clear to me if it is possible to export an
explicit specialization of a non-exported template function.

In the example below, "grabuge.hpp" declares a template and two
explicit specialization, one of which is defined (and exported) in
the file "grabuge.cpp" (and the second of which is never defined).

Is the example legal or not? Since the optional "export" keyword is
not listed in the grammar detailing explicit specialization, I would
guess the program is perhaps, after all, not legal. In that case, it
would appear to be a deficiency of the standard. The closest thing I
could find to that topic is clause 8, which does not seem to be
entirely relevant.

Merci

Hubert Holin


  8>< ------------ main.cpp ------------ ><8


#include "grabuge.hpp"



int main (int argc, char * const argv[])
{
     ::std::cout << "There are " << argc << " arguments, with values:
" << ::std::endl;

     for    (int idx = 0; idx < argc; ++idx)
     {
         switch    (idx)
         {
             case 0:

                 ::std::cout << grabuge<0> << argv[idx] << ::std::endl;

                 break;

             default:

                 ::std::cout << grabuge<-1> << argv[idx] << ::std::endl;

                 break;
         }
     }

     return 0;
}

  8>< --------------------------------- ><8

  8>< ---------- grabuge.hpp----------- ><8

#include <iostream>


template<int selector>
::std::ostream &    grabuge(::std::ostream & s)
{
     return(s);
}

template<>
::std::ostream &    grabuge<0>(::std::ostream & s);

template<>
::std::ostream &    grabuge<1>(::std::ostream & s);    // actually
not defined!

  8>< --------------------------------- ><8

  8>< ---------- grabuge.cpp----------- ><8


#include "grabuge.hpp"



export template<>
::std::ostream &    grabuge<0>(::std::ostream & s)
{
     return(s << "The 0th argument, by tradition, is: ");
}


  8>< --------------------------------- ><8

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: google@vandevoorde.com
Date: 22 Oct 2005 18:40:25 GMT
Raw View
Hubert Holin wrote:
> Somewhere in the E.U., le 21/10/2005
>
> Bonjour
>
> Reading 14.7.3, it is not clear to me if it is possible to export an
> explicit specialization of a non-exported template function.

No, the grammar doesn't allow it.  Only the grammar rule in 14/1
allows for the keyword "export".

> In the example below, "grabuge.hpp" declares a template and two
> explicit specialization, one of which is defined (and exported) in
> the file "grabuge.cpp" (and the second of which is never defined).
>
> Is the example legal or not? Since the optional "export" keyword is
> not listed in the grammar detailing explicit specialization, I would
> guess the program is perhaps, after all, not legal.

Right.

> In that case, it
> would appear to be a deficiency of the standard.

Can you elaborate?  Why is it a deficiency?

        Daveed

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Hubert Holin <Hubert.Holin@lmd.polytechnique.fr>
Date: Mon, 24 Oct 2005 10:25:15 CST
Raw View
Somewhere in the E.U., le 24/10/2005

     Bonjour

In article <1129933108.040492.203720@z14g2000cwz.googlegroups.com>,
  google@vandevoorde.com wrote:

 > Hubert Holin wrote:
 > > Somewhere in the E.U., le 21/10/2005
 > >
 > > Bonjour
 > >
 > > Reading 14.7.3, it is not clear to me if it is possible to
export an
 > > explicit specialization of a non-exported template function.

[SNIP]

 > > Is the example legal or not? Since the optional "export" keyword is
 > > not listed in the grammar detailing explicit specialization, I
would
 > > guess the program is perhaps, after all, not legal.
 >
 > Right.
 >
 > > In that case, it
 > > would appear to be a deficiency of the standard.
 >
 > Can you elaborate?  Why is it a deficiency?
 >
 >         Daveed
 >

         My belief that this represents a deficiency stems from the
assumption that the "export" keyword exists to provide some amount of
code modularity (in addition to potential code hiding): the user of
the template need only know the methods of the template, not their
implementation.

         I therefore find it odd that one can export general methods
(the unspecialized template), but not specific ones (the explicitly
specialized template).

         Certainly, the compiler has to be aware that these explicit
instantiations exist, but I fail to see how this differs from the
fact it has to be aware that the exported (unspecialized) template
exists.

         What am I missing?

     Merci

             Hubert Holin


---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ben-public-nospam@decadentplace.org.uk (Ben Hutchings)
Date: Tue, 25 Oct 2005 01:51:37 GMT
Raw View
Hubert Holin <Hubert.Holin@lmd.polytechnique.fr> wrote:
<snip>
>          My belief that this represents a deficiency stems from the
> assumption that the "export" keyword exists to provide some amount of
> code modularity (in addition to potential code hiding): the user of
> the template need only know the methods of the template, not their
> implementation.
>
>          I therefore find it odd that one can export general methods
> (the unspecialized template), but not specific ones (the explicitly
> specialized template).
>
>          Certainly, the compiler has to be aware that these explicit
> instantiations exist, but I fail to see how this differs from the
> fact it has to be aware that the exported (unspecialized) template
> exists.
>
>          What am I missing?

In practice, "export" causes the compiler to generate information
about a template definition in or alongside the object file that it
produces, so that the template can be implicitly instantiated between
the normal compilation process and linking (or by the linker calling
the compiler when it recognises an undefined symbol as being
satisfiable by such instantiation).  I assume that the standard
committee did not intend this to be done by default because it would
bloat object files generated from the compilation of source files that
use the existing model of including template definitions in every
translation unit that requires them, and perhaps also because it
could effectively reveal proprietary source code.

For full specialisations, this issue does not arise.  Linking to a
full specialisation of a function template works much like linking to
an ordinary function; callers only need the function's declaration and
(at link time) its object code.

--
Ben Hutchings
For every complex problem
there is a solution that is simple, neat, and wrong.

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: google@vandevoorde.com
Date: Mon, 24 Oct 2005 21:24:00 CST
Raw View
Hubert Holin wrote:
> Somewhere in the E.U., le 24/10/2005
>
>      Bonjour
>
> In article <1129933108.040492.203720@z14g2000cwz.googlegroups.com>,
>   google@vandevoorde.com wrote:
>
>  > Hubert Holin wrote:
>  > > Somewhere in the E.U., le 21/10/2005
>  > >
>  > > Bonjour
>  > >
>  > > Reading 14.7.3, it is not clear to me if it is possible to
> export an
>  > > explicit specialization of a non-exported template function.
>
> [SNIP]
>
>  > > Is the example legal or not? Since the optional "export" keyword is
>  > > not listed in the grammar detailing explicit specialization, I
> would
>  > > guess the program is perhaps, after all, not legal.
>  >
>  > Right.
>  >
>  > > In that case, it
>  > > would appear to be a deficiency of the standard.
>  >
>  > Can you elaborate?  Why is it a deficiency?
>  >
>  >         Daveed
>  >
>
>          My belief that this represents a deficiency stems from the
> assumption that the "export" keyword exists to provide some amount of
> code modularity (in addition to potential code hiding): the user of
> the template need only know the methods of the template, not their
> implementation.
>
>          I therefore find it odd that one can export general methods
> (the unspecialized template), but not specific ones (the explicitly
> specialized template).
>
>          Certainly, the compiler has to be aware that these explicit
> instantiations exist, but I fail to see how this differs from the
> fact it has to be aware that the exported (unspecialized) template
> exists.
>
>          What am I missing?

Explicit template specializations are not templates and can therefore
be separately compiled (or not) in the same way as their
nonparameterized
counterparts.  For example:

  // File f.hpp:
  template<typename T> void f() {}
  template<> void f<int>();  // Declaration only

  // File f.cpp:
  #include "f.hpp"

  template<> void f<int>() {}

The original intent was that template be separable in the exact same
way, with no special new keyword required (although you would have
been able to say "extern" as you can with ordinary functions and
variables).  The original Cfront implementation tried to fake this  by
making some assumptions about file organization.  However, that
scheme was not standardizable and for a long time the standard was
unclear about template declaration/definition separation.  Eventually
a compromise was developed that required separated templates to
be marked with "export".  For nontemplates (including explicit
specializations), that was not needed though.

        Daveed

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Hubert.Holin@lmd.polytechnique.fr (Hubert Holin)
Date: Thu, 27 Oct 2005 17:20:14 GMT
Raw View
Somewhere in the E.U., le 27/10/2005

     Bonjour

In article <1130181540.805807.160460@g44g2000cwa.googlegroups.com>,
  google@vandevoorde.com wrote:

[explanation of why "export" is not needed for explicit
specialization of templates]

         I see now your point. It is therefore not a deficiency in
the standard. Still some wording could be added (as a note,
preferably), so as to explain (as you did) why it is thus and to
prevent others from getting  confused.

     Merci

             Hubert Holin

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "kanze" <kanze@gabi-soft.fr>
Date: Mon, 31 Oct 2005 22:31:05 CST
Raw View
google@vandevoorde.com wrote:
> Hubert Holin wrote:
> > Somewhere in the E.U., le 24/10/2005

> > In article <1129933108.040492.203720@z14g2000cwz.googlegroups.com>,
> >   google@vandevoorde.com wrote:

> >  > Hubert Holin wrote:
> >  > > Somewhere in the E.U., le 21/10/2005

> >  > > Reading 14.7.3, it is not clear to me if it is possible
> >  > > to export an explicit specialization of a non-exported
> >  > > template function.

> > [SNIP]

> >  > > Is the example legal or not? Since the optional
> >  > > "export" keyword is not listed in the grammar detailing
> >  > > explicit specialization, I would guess the program is
> >  > > perhaps, after all, not legal.

> >  > Right.

> >  > > In that case, it would appear to be a deficiency of the
> >  > > standard.

> >  > Can you elaborate?  Why is it a deficiency?

> > My belief that this represents a deficiency stems from the
> > assumption that the "export" keyword exists to provide some
> > amount of code modularity (in addition to potential code
> > hiding): the user of the template need only know the methods
> > of the template, not their implementation.

> > I therefore find it odd that one can export general methods
> > (the unspecialized template), but not specific ones (the
> > explicitly specialized template).

> > Certainly, the compiler has to be aware that these explicit
> > instantiations exist, but I fail to see how this differs
> > from the fact it has to be aware that the exported
> > (unspecialized) template exists.

> >          What am I missing?

> Explicit template specializations are not templates and can
> therefore be separately compiled (or not) in the same way as
> their nonparameterized counterparts.  For example:

>   // File f.hpp:
>   template<typename T> void f() {}
>   template<> void f<int>();  // Declaration only

>   // File f.cpp:
>   #include "f.hpp"

>   template<> void f<int>() {}

Does this mean that, as with normal functions, providing a
definition in the .hpp file would result in undefined behavior
(supposing that the file was included in more than one
translation unit).

--
James Kanze                                           GABI Software
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: google@vandevoorde.com
Date: Wed, 2 Nov 2005 09:51:50 CST
Raw View
kanze wrote:
> google@vandevoorde.com wrote:
[...]
> >   // File f.hpp:
> >   template<typename T> void f() {}
> >   template<> void f<int>();  // Declaration only
>
> >   // File f.cpp:
> >   #include "f.hpp"
>
> >   template<> void f<int>() {}
>
> Does this mean that, as with normal functions, providing a
> definition in the .hpp file would result in undefined behavior
> (supposing that the file was included in more than one
> translation unit).

Indeed and it would in practice result in a duplicate symbol
linker error.  (Unless, of course, the specialization is an inline
function.)

        Daveed

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]