473,395 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

export: should I want it?

<quote
url="http://www.informit.com/guides/content.asp?g=cplusplus&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::ostream& 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 1983

Steven T. Hatton wrote:
<quote
url="http://www.informit.com/guides/content.asp?g=cplusplus&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::ostream& 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::ostream&) = 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::ostream& out);
private:
std::string data;
};

// MyClass.cpp
#include "MyClass.hpp"
MyClass::MyClass(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::ostringstream 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::ostream& 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::ostringstream 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...@germania.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)==sizeof(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
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...
0
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...
10
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...
205
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
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...
7
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...
0
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...
5
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
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.