Exporting templates

T

Tomás

I'm writing code at the moment which I intend to be 100% portable and
well-defined in line with the current C++ Standard.

I like the "export" feature, whereby I can put template functions in
source files (where I feel they belong -- unless I intend them to be
inline, of course).

I realise, however, that the majority of compilers are defective in that
they do not implement the "export" feature.

I see that the FAQ offers an alternative:

//header.hpp
template<class T>
T Get();

#ifdef UNSUPPORTED_EXPORT_KEYWORD
#include "header.cpp"
#endif


//header.cpp
#ifdef UNSUPPORTED_EXPORT_KEYWORD
#define export
#endif

export template<class T>
T Get()
{
return 0;
}


Do many of you here use this? I'm thinking of starting to use it in my
code. I'm going to put a "readme" file with it saying:

If your compiler doesn't implement the "export" feature, then define blah
blah...

While I'm at it, are there any othere things in the C++ Standard that
aren't done properly by a lot of compilers?

-Tomás
 
J

Jacek Dziedzic

Tomás said:
> [...]
I'm going to put a "readme" file with it saying:

If your compiler doesn't implement the "export" feature, then define blah
blah...

Try 'if your compiler implements the "export" feature,
then _undefine_ blah blah'. Compilers that support 'export'
are in the minority :)

- J.
 
G

Gavin Deane

Tomás said:
I'm writing code at the moment which I intend to be 100% portable and
well-defined in line with the current C++ Standard.

Do many of you here use this? I'm thinking of starting to use it in my
code. I'm going to put a "readme" file with it saying:

If your compiler doesn't implement the "export" feature, then define blah
blah...

Having never used the technique I can't usefully comment on it, however
....
While I'm at it, are there any othere things in the C++ Standard that
aren't done properly by a lot of compilers?

.... if you are trying to write portable and conformant code, one issue
you may run into is the <cxxx> vs <xxx.h> C library headers. Quite a
lot of compilers (MSVC++ 8, Comeau online last time I checked, GCC
3.4.2 in Dev C++) implement the <cxxx> headers incorrectly in that C
library names are placed in the global namespace as well as namespace
std. So if you forget to prepend printf with std:: your code will be
incorrect but the compiler won't complain.

Gavin Deane
 
R

Rolf Magnus

Tomás said:
I'm writing code at the moment which I intend to be 100% portable and
well-defined in line with the current C++ Standard.

I like the "export" feature, whereby I can put template functions in
source files (where I feel they belong -- unless I intend them to be
inline, of course).

I realise, however, that the majority of compilers are defective in that
they do not implement the "export" feature.

One could argue that it might be the standard that is defective. If the
majority of compilers don't implement that feature even 7 years after the
standard was released, this indicates that this feature is problematic.
I see that the FAQ offers an alternative:

//header.hpp
template<class T>
T Get();

#ifdef UNSUPPORTED_EXPORT_KEYWORD
#include "header.cpp"
#endif


//header.cpp
#ifdef UNSUPPORTED_EXPORT_KEYWORD
#define export
#endif

export template<class T>
T Get()
{
return 0;
}


Do many of you here use this?

I don't see any real advantage in this. Usually, you do such things if
several compilers must be treated differently, but the non-export version
works on all compilers, so IMHO, this just adds unnecessary clutter.
While I'm at it, are there any othere things in the C++ Standard that
aren't done properly by a lot of compilers?

Maybe two-phase lookup.
 
D

Daniel T.

"Tomás said:
I'm writing code at the moment which I intend to be 100% portable and
well-defined in line with the current C++ Standard.

Do it like they do it at "boost.org". They have several defines to get
around compiler problems already in their library. Use them and maybe
one day, you can submit your stuff for inclusion in their library.
I see that the FAQ offers an alternative:

//header.hpp
template<class T>
T Get();

#ifdef UNSUPPORTED_EXPORT_KEYWORD
#include "header.cpp"
#endif


//header.cpp
#ifdef UNSUPPORTED_EXPORT_KEYWORD
#define export
#endif

export template<class T>
T Get()
{
return 0;
}


boost.org doesn't do the above...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,133
Latest member
MDACVReview
Top