473,890 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

export: should I want it?

<quote
url="http://www.informit.co m/guides/content.asp?g=c plusplus&seqNum =53&rl=1">

exported Templates

Last updated Sep 6, 2006.
exported Templates

The separate compilation model enables us to define functions, types and
objects in one translation unit and refer to them from other translation
units. After compiling all the translation units of a program, the linker
subsequently resolves all the references to extern symbols, producing a
single executable file. When dealing with ordinary functions and classes,
the separate compilation model works neatly.

Templates, however are a different beast. When you instantiate a template,
the compiler must see its definition. This is an explicit requirement in
the C++ standard, which states that dependent names (names that depend on
the type of a template parameter) shall be looked up in both the current
context (i.e., the place of instantiation) and the context of the
template's definition. The compiler cannot generate the code of a
specialization without looking at the template's definition ? even if the
definition appears in a separate translation unit. If the template is
defined in several source files, all the definitions must comply with the
One Definition Rule. As I explained elsewhere, the common approach is to
define the template in a header file and #include it in every source file
that uses that template.

If you have used templates before, you already know that. However, standard
C++ defines another compilation model for templates known as the separation
model.
export: How it All Began

Right from the start, C++ creators called for separate compilation of
templates. However, they didn't specify how this could be accomplished.
Unlike most other C++ features, which were standardized only after being
implemented by one vendor at least, exported templates were added to C++ in
1996 with no existing implementation whatsoever.

In 1996, many compilers hardly supported ordinary templates and the C++
community had gained little experience with advanced template techniques
such as meta-programming. Therefore, the implications of implementing
export weren't fully recognized. The very few people who have implemented
exported templates since agree that this is an arduous task indeed.
State of the Art

The most popular C++ compilers (Microsoft's Visual C++ .NET, Borland's C++
BuilderX, GNU's GCC and others) don't support export. Even vendors who have
expressed their commitment to "100% ISO compliance" exclude export from
their checklist. The frequently-cited reason for this is "lack of demand."
Could it be that users don't demand this features because they've never had
a chance to use it?

....
</quote>

One view of the "proper" use of "header files" is that they
represent "interfaces ", whereas "source files" represent implementations .
Indeed some people suggest that we not have anything requiring allocation
in a header file; with the exception of (judiciously chosen) inline member
functions, and class template members[*]. That means, according to the
advice, MyClass::_data doesn't belong in a header.

class MyClass {
public:
std::ostream& write(std::ostr eam& out);
private:
std::string _data; //I don't belong here?
};

I have to agree that I gain a modest sense of accomplishment if I write a
class definition in a header file, and the only #include needed is to
include the definition of the base class. Any other types named can be
introduced using a forward declaration.

So what has this to do with `export'? Well, without it, all of a template
definition must go in a header file, or directly in the source file being
compiled. The motivations I am aware of for not wanting allocation defined
in a header are so that code compiled using the header won't need to be
recompiled if the implementation of the interface declared in the header
changes. Another reason is that it speeds up compile time. I'm not sure
any of that would be gained in the case of processing template code as a
direct result of implementing export. For example, would using precompiled
headers provide all the compile speed advantages that would be gained by
implementing export?

What would I gain if my compiler supported export?

[*]Of course we are also admonished not to "second guess" the implementors
of the (Standard) Library by forward declaring entities defined therein.
IOW I shouldn't try to forward declare std::string like this:

namespace std {
class string; //Wrong! Mere users should not mess with std::
}

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 3 '06 #1
4 2009

Steven T. Hatton wrote:
<quote
url="http://www.informit.co m/guides/content.asp?g=c plusplus&seqNum =53&rl=1">

exported Templates

Last updated Sep 6, 2006.
exported Templates

The separate compilation model enables us to define functions, types and
objects in one translation unit and refer to them from other translation
units. After compiling all the translation units of a program, the linker
subsequently resolves all the references to extern symbols, producing a
single executable file. When dealing with ordinary functions and classes,
the separate compilation model works neatly.

Templates, however are a different beast. When you instantiate a template,
the compiler must see its definition. This is an explicit requirement in
the C++ standard, which states that dependent names (names that depend on
the type of a template parameter) shall be looked up in both the current
context (i.e., the place of instantiation) and the context of the
template's definition. The compiler cannot generate the code of a
specialization without looking at the template's definition ? even if the
definition appears in a separate translation unit. If the template is
defined in several source files, all the definitions must comply with the
One Definition Rule. As I explained elsewhere, the common approach is to
define the template in a header file and #include it in every source file
that uses that template.

If you have used templates before, you already know that. However, standard
C++ defines another compilation model for templates known as the separation
model.
export: How it All Began

Right from the start, C++ creators called for separate compilation of
templates. However, they didn't specify how this could be accomplished.
Unlike most other C++ features, which were standardized only after being
implemented by one vendor at least, exported templates were added to C++ in
1996 with no existing implementation whatsoever.

In 1996, many compilers hardly supported ordinary templates and the C++
community had gained little experience with advanced template techniques
such as meta-programming. Therefore, the implications of implementing
export weren't fully recognized. The very few people who have implemented
exported templates since agree that this is an arduous task indeed.
State of the Art

The most popular C++ compilers (Microsoft's Visual C++ .NET, Borland's C++
BuilderX, GNU's GCC and others) don't support export. Even vendors who have
expressed their commitment to "100% ISO compliance" exclude export from
their checklist. The frequently-cited reason for this is "lack of demand."
Could it be that users don't demand this features because they've never had
a chance to use it?

...
</quote>

One view of the "proper" use of "header files" is that they
represent "interfaces ", whereas "source files" represent implementations .
Indeed some people suggest that we not have anything requiring allocation
in a header file; with the exception of (judiciously chosen) inline member
functions, and class template members[*]. That means, according to the
advice, MyClass::_data doesn't belong in a header.

class MyClass {
public:
std::ostream& write(std::ostr eam& out);
private:
std::string _data; //I don't belong here?
};
What makes you think that _data is allocated here? Its not. The
compiler will generate a ctor and implement the allocation of _data
whether or not you declare/define any ctors. Also, from the compiler's
perspective, the header does not exist. The compilation unit, probably
MyClass.cpp in this case, will have the above header injected into it.

As far as your discussion of "interfaces " above, it sounds more like
you are talking about abstract classes. A corresponding example would
be:

// mybase.hpp
#include <ostream>

struct MyBase // abstract
{
virtual ~MyBase();
std::ostream& write(std::ostr eam&) = 0;
};

// MyBase.cpp
#include "MyBase.hpp "

MyBase::~MyBase () { }

// MyClass.hpp
#include <string>
#include "MyBase.hpp "

class MyClass : public MyBase
{
public:
MyClass();
std::ostream& write(std::ostr eam& out);
private:
std::string data;
};

// MyClass.cpp
#include "MyClass.hp p"
MyClass::MyClas s(const std::string& r_s) : data(r_s)
{
}

std::ostream& MyClass::write( std::ostream& out)
{
...
}
>
I have to agree that I gain a modest sense of accomplishment if I write a
class definition in a header file, and the only #include needed is to
include the definition of the base class. Any other types named can be
introduced using a forward declaration.

So what has this to do with `export'? Well, without it, all of a template
definition must go in a header file, or directly in the source file being
compiled. The motivations I am aware of for not wanting allocation defined
in a header are so that code compiled using the header won't need to be
recompiled if the implementation of the interface declared in the header
changes. Another reason is that it speeds up compile time. I'm not sure
any of that would be gained in the case of processing template code as a
direct result of implementing export. For example, would using precompiled
headers provide all the compile speed advantages that would be gained by
implementing export?
Consider an often used template like:

// ttostring.hpp
#ifndef TTOSTRING_HPP_
#define TTOSTRING_HPP_
#include <string>
#include <sstream>

template< typename T >
std::string TtoString( const T& r_t )
{
std::ostringstr eam oss;
oss << r_t;
return oss.str()
}

#endif

A different version or version(s) of that template will be generated in
whatever compilation unit uses it.
>
What would I gain if my compiler supported export?

[*]Of course we are also admonished not to "second guess" the implementors
of the (Standard) Library by forward declaring entities defined therein.
IOW I shouldn't try to forward declare std::string like this:

namespace std {
class string; //Wrong! Mere users should not mess with std::
}

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 3 '06 #2
Salt_Peter wrote:
>
Steven T. Hatton wrote:
>One view of the "proper" use of "header files" is that they
represent "interfaces ", whereas "source files" represent implementations .
Indeed some people suggest that we not have anything requiring allocation
in a header file; with the exception of (judiciously chosen) inline
member
functions, and class template members[*]. That means, according to the
advice, MyClass::_data doesn't belong in a header.

class MyClass {
public:
std::ostream& write(std::ostr eam& out);
private:
std::string _data; //I don't belong here?
};

What makes you think that _data is allocated here?
"C++ allows the programmer to expose the representation of a class as part
of the interface. This representation may be hidden (using /private/
or /protected/), but it is available to the compiler to allow alocation of
automatic vaiables, to allow inline substitution of functions, etc." -
TC++PL(SE).
Its not. The
compiler will generate a ctor and implement the allocation of _data
whether or not you declare/define any ctors.
The term "allocation " is used to describe the operation performed by the
compiler which determines the beginning and ending relative offsets of a
block of storage reserved to store an instance of a variable.
Also, from the compiler's
perspective, the header does not exist. The compilation unit, probably
MyClass.cpp in this case, will have the above header injected into it.
What about

//YourClass.cpp
#include <sth/MyClass>

?
As far as your discussion of "interfaces " above, it sounds more like
you are talking about abstract classes. A corresponding example would
be:

// mybase.hpp
#include <ostream>
#include <iosfwd>
>Another reason is that it speeds up compile time. I'm not sure
any of that would be gained in the case of processing template code as a
direct result of implementing export. For example, would using
precompiled headers provide all the compile speed advantages that would
be gained by implementing export?

Consider an often used template like:

// ttostring.hpp
#ifndef TTOSTRING_HPP_
#define TTOSTRING_HPP_
#include <string>
#include <sstream>

template< typename T >
std::string TtoString( const T& r_t )
{
std::ostringstr eam oss;
oss << r_t;
return oss.str()
}

#endif

A different version or version(s) of that template will be generated in
whatever compilation unit uses it.
I believe you mean to say that the template will be instantiated differently
in different compilation units depending upon the actual template
parameters used to instantiate it. How is that impacted by `export'?
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Dec 4 '06 #3
On Dec 4, 3:14 am, "Steven T. Hatton" <chatten...@ger mania.supwrote:
Salt_Peter wrote:
A different version or version(s) of that template will be generated in
whatever compilation unit uses it.

I believe you mean to say that the template will be instantiated differently
in different compilation units depending upon the actual template
parameters used to instantiate it. How is that impacted by `export'?
With export the template will only be compiled/instanciated once, and
that is in the compilation-unit of the template implementation. This
single implementation is then linked in and used wherever the template
is instanciated. This should give smaler executables, if nothing else,
since the same code is not duplicated. In template-heave programs this
can give significant space-savings, and might lead to faster
execution-times too, by better usage of the cache.

Well, at least that's how I've understand things.

--
Erik Wikström

Dec 4 '06 #4
er****@student. chalmers.se schreef:
With export the template will only be compiled/instanciated once, and
that is in the compilation-unit of the template implementation. This
single implementation is then linked in and used wherever the template
is instanciated. This should give smaler executables, if nothing else,
since the same code is not duplicated. In template-heave programs this
can give significant space-savings, and might lead to faster
execution-times too, by better usage of the cache.
Actually, a modern compiler will remove duplicate functions at link
time, even if the
types originally were different. Enough compilers have
sizeof(int)==si zeof(long);
instantiating function templates twice when they're overloaded on both
may be
required but the result need not be linked in twice. Export is not
needed for that.

HTH,
Michiel Salters

Dec 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2377
by: Bennett Haselton | last post by:
I'm working on an ASP.Net project where I want to test code on a local machine using a local database as a back-end, and then export it to the production machine where it uses the hosting provider's SQL Server database on the back-end. Is there a way to export tables from one SQL Server database to another in such a way that if a table already exists in the destination database, it will be updated to reflect the changes to the local...
0
1905
by: Shawn Mehaffie | last post by:
I have the following class that I've wirtten to take a Dataset and automatically export it to either XML, ASCII or Tab delimited file. The reason I wrote it they way I did was that I don't want to have o create a temporary file on the web server everytime someone want to run and export. When I call the ExportToBrowser function the user gets the standard open/save file dialog box. If the user chooses to save the file the program works...
10
1969
by: New_user | last post by:
Hello. Excerpt from ISO/IEC 14882 : 14/6 A namespace-scope declaration or definition of ........skipped...... a non- inline member function of a class template or a static data member of a class template may be preceded by the export keyword. and from http://www.comeaucomputing.com/4.3.0/minor/win95+/43stuff.txt : A template must be declared export at both the point of definition and
205
10778
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
5
11059
by: Tim Eliot | last post by:
Just wondering if anyone has hit the following issue and how you might have sorted it out. I am using the command: DoCmd.TransferText acExportMerge, , stDataSource, stFileName, True after setting stDataSource and stFileName to the desired values. Most of the time it works, but occasionally, typically as code changes are being made to the module, the following message appears:
7
14389
by: Pat | last post by:
I would like to send the Print Preview of a MS Access form to a Snapshot file. The form contains an OLE graph. BACKGROUND A snapshot of a report is possible. If I could I would use a report to display the graph but it is not possible to send query parameters to an OLE graph on a report (or programatically change any properties of an OLE Graph). However with a Form you do have the ability to change the RowSource of
0
1519
by: Shawn Mehaffie | last post by:
I have the following class that I've wirtten to take a Dataset and automatically export it to either XML, ASCII or Tab delimited file. The reason I wrote it they way I did was that I don't want to have o create a temporary file on the web server everytime someone want to run and export. When I call the ExportToBrowser function the user gets the standard open/save file dialog box. If the user chooses to save the file the program works fine,...
5
31933
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
5
3454
by: JHNielson | last post by:
I have a somewhat simple question, but have been baffled by it for a while, and now I'm on a tight deadline - have to get it done within 24 hours. I am trying to export a set of files to my hard drive to then later be FTPd to a server. the file format should be "TITLE.yyyymmdd" where yymmdd is the date code for when the files are created. The files can't be .txt. But when I run the files as ".txt" the code runs just fine. when I...
1
10524
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am trying to export to Excel using a command in an Access Form. RowID strFY AccountID CostElementWBS 1 2008 1 7 2 2008 1 7 I want to...
0
9978
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9822
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8016
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7169
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5851
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4271
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.