Topic: Export keyword and templates


Author: Vesselin Ivanov <ivanov@universal.ca>
Date: 1997/06/18
Raw View
Can enyone help me understand the meaning, intended use and status of the
keyword "export"?

The section of the standard on templates contains the following text:

6 A non-inline template function or a static  data  member  template  is
  called  an exported template if its definition is preceded by the key-
  word export or if it has been previously declared  using  the  keyword
  export  in  the  same  translation  unit.   Declaring a class template
  exported is equivalent to declaring all  of  its  non-inline  function
  members,  static  data  members, member classes, and non-inline member
  templates which are defined in that translation unit exported.


Yet, the section of the same document on key words does NOT list such a
key word.


Thanks a lot.

Vesselin Ivanov

ivanov@universal.ca  or
yanita@nbnet.nb.ca
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: David Vandevoorde <daveed@vandevoorde.com>
Date: 1997/06/18
Raw View
Vesselin Ivanov wrote:
>
> Can enyone help me understand the meaning, intended use and status
> of the keyword "export"?

Let me try. The 'export' keyword is an indication to the compiler
that it may need to save a semi-bound form of certain template
definition. By "semi-bound" I mean that the non-dependent names
(those that do not depend on the template arguments) will have
been looked up and associated with the entities these lookups
produced.

At a point of instantiation for these templates, it is not
required that the template definition be visible, and instead
the implementation will automagically retrieve the semi-bound
version from wherever it saved it and complete the bindings
for the remaining names.

E.g.:

// file1.C
 static int a = 3;
 export template<class T> int f() {
    return a + T::a;
 }

// file2.C
 #include <stdio.h>
 struct X {
    enum { a = 32 };
 };
 template<class T> int f(); // no definition
 int main() {
    printf("%d\n", f<X>());
 }

should print out 35 followed by a new-line.

[...]
> Yet, the section of the same document on key words does NOT list
> such a key word.

An oversight, I believe. It's a known issue.

 Daveed
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: James Kuyper <kuyper@wizard.net>
Date: 1997/06/19
Raw View
Vesselin Ivanov wrote:
>
> Can enyone help me understand the meaning, intended use and status of the
> keyword "export"?

It is contained in Section 14, paragraph 7:

| ... An exported template
| need only be declared (and not necessarily defined) in  a  translation
| unit  in  which it is instantiated. ...

I don't understand templates well enough to know why the existing
keyword 'extern' was not used for this purpose.

> The section of the standard on templates contains the following text:
>
> 6 A non-inline template function or a static  data  member  template  is
>   called  an exported template if its definition is preceded by the key-
>   word export or if it has been previously declared  using  the  keyword
>   export  in  the  same  translation  unit.   Declaring a class template
>   exported is equivalent to declaring all  of  its  non-inline  function
>   members,  static  data  members, member classes, and non-inline member
>   templates which are defined in that translation unit exported.
>
> Yet, the section of the same document on key words does NOT list such a
> key word.

You're right, I think that is a defect. However, Section 2.11 paragraph
1 says:

| The identifiers shown in Table 3 are  reserved  for  use  as  keywords
| (that is, they are unconditionally treated as keywords in phase 7):

Perhaps it was intended that would be other keywords which are only
conditionaly treated as keywords, and 'export' is one of them? I doubt
it, but the standard is too complicated a document for me to be sure.
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: David Vandevoorde <daveed@vandevoorde.com>
Date: 1997/06/19
Raw View
James Kuyper wrote:
[...]
> I don't understand templates well enough to know why the
> existing keyword 'extern' was not used for this purpose.

It was very seriously considered, and many would still
prefer it. However, ultimately a majority of the voting
members of the committee found the arguments in favor
of distinguishing this mechanism from those associated
with `extern' compelling enough to warrant a new keyword.

It's probably relatively unimportant (though there are
environments that would rather reserve the word `export'
for other purposes).

 Daveed
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]