473,672 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Order of template specializations

Hi,

I have a templated class field:
template <typename Value_Type>
class Field
{
public:
Value_Type value;
};

I want to load the value member from an std::istream.
I want to use a special algorithm for string fields,
but the extraction operators for all others.

My question is: does the overloaded function have
to be declared before the template version?

void LoadField(Field <string>& sf, istream & inp)
{
// ...
}

template <typename Value_Type>
void LoadField(Field <Value_Type>& vf, istream & inp)
{
// ...
}

I would like to create specializations for the
Field<string> and place them in a different
translation unit than the one in which Field is
defined.

The idea is that when somebody wants a Field<double>
they don't get all the function declarations for
Field<string>.

This example was simplified from my actual code.
My actual design is:
Field
^
|
Typed_Field (template)
^
|
Sortable_Typed_ Field

typedef Sortable_Typed_ Field<int> Integer_Field;
typedef Sortable_Typed_ Field<string> String_Field;

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #1
1 1262
Thomas Matthews wrote in news:73t%b.5013 7$hd3.31340
@newssvr33.news .prodigy.com:
Hi,

I have a templated class field:
template <typename Value_Type>
class Field
{
public:
Value_Type value;
};

I want to load the value member from an std::istream.
I want to use a special algorithm for string fields,
but the extraction operators for all others.

My question is: does the overloaded function have
to be declared before the template version?

No, but it needs to be declared in all TU's that might use it.
void LoadField(Field <string>& sf, istream & inp)
{
// ...
}

template <typename Value_Type>
void LoadField(Field <Value_Type>& vf, istream & inp)
{
// ...
}

I would like to create specializations for the
Field<string> and place them in a different
translation unit than the one in which Field is
defined.
If they're overloads of fully explicit specialization' s then
declare them in a header, and define them elsewhere.

The idea is that when somebody wants a Field<double>
they don't get all the function declarations for
Field<string>.


Just in case:

- A declaration:

int f();

- A Defenition (also a declaration):

int f()
{
}

They need to see all possible declaration's but not all
defenition's.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

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

Similar topics

6
1769
by: NKOBAYE027 | last post by:
FIRST POST Hi All: I'm trying to write a simple specialization before moving on to something a bit more complex - always a good idea in my case, at least. :o) I'm trying to adapt the example from Stroustrup, 3rd ed., The C++ Programming Language p. 344 I'm using MSDev 6.0 in case that's an issue. Here's the source...
1
1570
by: Imre | last post by:
Let's suppose we have a primary template with one argument defined in a header file. Two source files include this header, and both define a specialization of the primary template. Later, both sources reference the template, using the same template argument, that matches both specializations. Now, in theory, both sources reference the same template with the same argument, but due to the different specializations in the sources, the exact...
1
1629
by: Samee Zahur | last post by:
Why aren't we allowed to do partial specializations like these on numeric template parameters? That would have allowed us to do all kinds of interesting stuffs like loops of variable nesting (bad practice though - I Know) and lots of other things!g++ even knows that it is a partial specialization I am aiming for ... but it just won't allow it! //----------------------------------------------------------------- template <unsigned...
4
1849
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
2
2189
by: Joseph Turian | last post by:
I'm posting this question for a friend who lacks USENET access. He and I were discussing this question and could not figure out the solution. Thank you for your help Joseph ===
4
3351
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) { //default ToString handles integers/doubles
16
1643
by: Hendrik Schober | last post by:
Hi, suppose we have template< typename T > struct X; and some specializations: template<>
1
2201
by: jason.cipriani | last post by:
Here is an example with 3 files, containing a template structure and also a template function. The header A.h declares a template structure A with a default (i.e. for any template parameter), inlined constructor implementation, and also declares a template function f. The file main.cpp contains some test code and also the default implementation for f(). The file spec.cpp contains some explicitly specialized stuff: ==== A.h ====
5
2379
by: (2b|!2b)==? | last post by:
I would like to know if I can specialize only a specific method for a class template. Is the (specialization) code below valid? template <typename T1, typename T2> class MyClass { public:
0
8404
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
8931
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8828
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8608
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8680
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...
1
6238
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
4227
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...
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.