473,402 Members | 2,055 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,402 software developers and data experts.

Concerning Partial Template Specialization

Hello,

First the code :)
///////////////////////////////////////////////////////////////////////////////////
// in another header file
namespace LJC{

struct DomainInterface {
... // pure virtual functions
};

struct ConnectionTypeInterface {
... // pure virtual functions
};


struct Domain {
struct Local : public DomainInterface
{
...
};
struct Internet : public DomainInterface
{
...
};
};

struct ConnectionType {
struct Stream : public ConnectionTypeInterface
{
...
};
struct Datagram : public ConnectionTypeInterface
{
...
};
};

.....

}; // end of namespace


///////////////////////////////////////////////////////////////////////////////////


template <
class DomainInterface,
class ConnectionTypeInterface
>
class Connection{
public:

Connection(const int port);
Connection(const char* address);

int init();

private:
DomainInterface Domain;
ConnectionTypeInterface ConnectionType;
};

// This works.
template <>
inline Connection<
LJC::Domain::Internet,
LJC::ConnectionType::Datagram
>::Connection(const char* a)
{
}

// This works.
template <
class DomainInterface,
class ConnectionTypeInterface
>
inline Connection<DomainInterface,
ConnectionTypeInterface>::Connection(const char* a)
{
}

// This does not work with the error message:
// invalid use of incomplete type ‘class
Connection<LJC::Domain::Internet, ConnectionTypeInterface>’
// declaration of ‘class Connection<LJC::Domain::Internet,
ConnectionTypeInterface>’
template <
class ConnectionTypeInterface
>
inline Connection<
LJC::Domain::Internet,
ConnectionTypeInterface
>::Connection(const char* a)
{
}


// This works
template <
class DomainInterface,
class ConnectionTypeInterface
>
int Connection<DomainInterface, ConnectionTypeInterface>::init()
{
}
///////////////////////////////////////////////////////////////////////////////////

To give some background on what I am trying to do:
I have recently created some TCP, UDP and UnixSocket libraries with
very similar interface for different projects. After getting inspired
by Alexandrescu's book, I was trying to combine those in a single
templated 'Connection' class, which would use such templates to define
protocols and policies (like server/client, blocking/non-blocking
function calls). The general flow of either of these combination is
similar, so I figured I would train myself into creating something
like this.

The specific problem I was trying to solve is the constructor call. A
class instantiated with Domain::Local should be constructed with a
const char* (socket path), but a class instantiated with
Domain::Internet should use an integer (port). To solve this, I though
I would create two constructors, one with char* and one with int. I
would implement a partial template specialization for <Internet,int>
and one for <Local,char*>, so if a user would call it with correct
combination it would work, but if he tried to call a wrong one it
would throw a compiler undefined reference error.

I looked around the web, some books etc, but I could not understand
why am I getting this error. I can use the constructor unspecialized
or fully specialized, but not partially. Full specialization is not
really acceptable since I plan to add more template parameters later
(starting of slow). Most examples I found on the net use ints or bools
while partially specializing, I tried with those but I got the same
result (I am saying this because I stumbled across someone mentioning
that I couldn't use a class for specialization, but I am confused
about what he meant).

I also read something else on the net, which I did not fully
understand; the partial specialization failed because the top-level
class is templated for <class DomainInterface, class
ConnectionTypeInterface>, and there is no top-level class
specialization for the partial-specialized case. Following this, I
tried (with several combinations) something like this:

template <
class ConnectionTypeInterface
>
class Connection{
public:

Connection(const int port);
Connection(const char* address);
....
};

But I got redefinition error messages.

Can what I describe above be done? If so, what is my mistake and how
can it be corrected? My C++ intuition so far tells me that it does,
but if it's impossible, I haven't thought of a secondary plan so far,
so I might ask later on :P
Jun 27 '08 #1
1 2190
Apparently, the Google groups have eaten some ">" that end the
template parameter lists when on a single line.

(And by the way, I use gcc 4.3.0, and the manpage didn't mention
anything about not supporting partial specializations, so I think/hope
it's not a compiler error).
Jun 27 '08 #2

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

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
9
by: Philip Lawatsch | last post by:
Hi I'd like to implement some kind if type traits myself, but I have to support broken compilers (like visual studio) that do not support Partial Specialization. My first shot was something...
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
1
by: BekTek | last post by:
I'm still confused about the template partial specialization which is used in many libraries.. due to lack of introduction for beginner.. Could you tell me about that in short? Thanks in...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
9
by: Greg | last post by:
Hi, I would like to specify behavior of a class member relatively to template implemetation. It works in usual cases but it seems to fail with to templates when one of the two is specified... ...
10
by: jason.cipriani | last post by:
I never seem to be able to get this right. Here I have some code: template <typename T, int Nclass A { void f (T); }; template <typename Tvoid A<T,1>::f (T) { } template <typename Tvoid...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...
0
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,...
0
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...

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.