473,657 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

partial specialized templated classes

Hi,

I'm working on a thin libcurl layer. Therefore I have the following classes:

template<typena me T, long CURLOPT_ID>
class CurlOption: boost::noncopya ble {
public:
typedef typename boost::call_tra its<T>::param_t ype param_type;
....
public:
// constructors:
CurlOption() {}
CurlOption(para m_type v) : m_value(v) {}

public:
long option() const { return CURLOPT_ID; }
result_type parameter() { return m_value; }
void setopt(CURL* handle);

private:
value_type m_value;
};

and the specialized for the templated arguments T (ether bool, long,
std::string etc.):

template<long CURLOPT_ID>
class CurlOption<bool , CURLOPT_ID{
public:
void setopt(CURL* handle) {
curl_easy_setop t(handle,
this->option(), this->value() ? 1 : 0);
}
};

e.g. used like

struct CurlOpt {
typedef CurlOption<bool , CURLOPT_VERBOSE > Verbose;

};

and than the cURL handle class self using a double dispatch approach:

class EasyCurl : boost::noncopya ble
{
public:
...
template<typena me T, long ID>
void setopt(const CurlOption<T, ID>& opt) {
opt.setopt(m_cu rl);
}

private:
CURL* m_curl;
};
By use of this, e.g.

CurlOpt::Verbos e v(on);

I get a compiler error:

EasyCurl.cpp: In member function »void EasyCurl::setVe rbose(bool)«:
EasyCurl.cpp:12 4: Fehler: keine passende Funktion für Aufruf von
CurlOption<bool , 41l>::CurlOptio n(bool&)«
EasyCurl.hpp:19 : Anmerkung: Kandidaten sind: CurlOption<bool ,
41l>::CurlOptio n()
EasyCurl.hpp:19 : Anmerkung: CurlOption<bool , 41l>::CurlOptio n(const
CurlOption<bool , 41l>&)

Obviously the right ctor is missing. Anyway, do I have all the stuff
from the Template (typedefs including) to rewrite for the specialized
template class? Or is there another way? I thought I need only to
overwrite the specialized members - where is my mistake? Is there a
better and correct way?

Thanks
Olaf

Jul 24 '07 #1
2 1862
Olaf wrote:
[..] I have the following
classes:

template<typena me T, long CURLOPT_ID>
class CurlOption: boost::noncopya ble {
public:
typedef typename boost::call_tra its<T>::param_t ype param_type;
....
public:
// constructors:
CurlOption() {}
CurlOption(para m_type v) : m_value(v) {}

public:
long option() const { return CURLOPT_ID; }
result_type parameter() { return m_value; }
void setopt(CURL* handle);

private:
value_type m_value;
};

and the specialized for the templated arguments T (ether bool, long,
std::string etc.):

template<long CURLOPT_ID>
class CurlOption<bool , CURLOPT_ID{
public:
void setopt(CURL* handle) {
curl_easy_setop t(handle,
this->option(), this->value() ? 1 : 0);
}
};

e.g. used like

[.. some errors, with the message in German, I can't read German ..]

Obviously the right ctor is missing. Anyway, do I have all the stuff
from the Template (typedefs including) to rewrite for the specialized
template class?
Of course.
Or is there another way?
Not really.
I thought I need only to
overwrite the specialized members - where is my mistake?
Your mistake in thinking that a template specialisation contains all
stuff from the unspecialised one except the stuff you [re]define. It
does not.
Is there a
better and correct way?
Inheritance, maybe? You still can't inherit constructors, though.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 24 '07 #2
>I thought I need only to
>overwrite the specialized members - where is my mistake?

Your mistake in thinking that a template specialisation contains all
stuff from the unspecialised one except the stuff you [re]define. It
does not.
>Is there a
better and correct way?

Inheritance, maybe? You still can't inherit constructors, though.
Well, I solved the problem using traits (finally I have to write about 5
specialized templates). Attached the result. Maybe there are better
ways? Class Option must be copyable by use of a temporary for Verbose
(in main). Why? Maybe better solutions here?

Thanks
Olaf

#include <iostream>
#include <string>
#include <boost/utility.hpp>
#include <boost/call_traits.hpp >
using namespace std;

enum {
VERBOSE_ID,
FILE_ID
};

template<typena me Tstruct OptionTraits : public boost::noncopya ble { };

template<>
struct OptionTraits<bo ol: public boost::noncopya ble
{
static long get(bool value) { return value; }
static std::string type() { return "bool"; }
};

template<>
struct OptionTraits<st d::string: public boost::noncopya ble
{
static const char* get(const std::string& value) {
return value.c_str();
}
static std::string type() { return "string"; }
};

template<typena me T, long ID>
class Option // : public boost::noncopya ble
{
public:
typedef typename boost::call_tra its<T>::param_t ype param_type;
typedef T value_type;
typedef typename boost::call_tra its<T>::value_t ype result_type;

public:
Option(param_ty pe v) : m_value(v) {}

public:
long option() const { return ID; }
result_type parameter() { return m_value; }

public:
void setopt(int /*handle*/) const {
// work with handle; not relevant for problem here
// required is ID + value (and T bool,string etc. of course)
// for legacy API
cout << "option<"
<< OptionTraits<T> ::type() << ", " << ID << ">: "
<< OptionTraits<T> ::get(m_value) << "\n";
}
private:
value_type m_value;
};
typedef Option<bool, VERBOSE_ID> Verbose;
typedef Option<std::str ing, FILE_IDFile;
class Foo : boost::noncopya ble
{
public:
Foo() : m_handle(0) {}

public:
void perform();

template<typena me T, long ID>
void setopt(const Option<T, ID>& opt) {
opt.setopt(m_ha ndle);
}
private:
int m_handle;
};

int main()
{
//Verbose v(true);
File f("bar.txt");

Foo foo;
foo.setopt(Verb ose(true));
foo.setopt(f);
}
Jul 25 '07 #3

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

Similar topics

17
6849
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 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
5
6579
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>
7
2097
by: Kai-Uwe Bux | last post by:
Hi folks, I observed something that puzzles me. When I do namespace xxx { using std::swap; } it appears that xxx::swap and std::swap are not strictly equivalent. In particular, I think that my implementation will only choose the partial specialization for std::vector when std::swap is used.
8
4794
by: Ferdi Smit | last post by:
I've never understood the rationale of allowing partial, but not explicit specialization for classes at non-namespace scope. Ie.: struct A { template <typename T1, typename T2> struct B {}; // this is not allowed: template <> struct B<int, float> {};
4
1494
by: wakun | last post by:
Hi there, I am learning template programming. When testing the partial specialization, I have some probelms Here is a full templated class template <typename T, int n> class CT { public: T data;
6
2760
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 no errors compiling this:
1
2631
by: Martin | last post by:
I have a couple of partial specializations of a templated class, but my code results in "undefined reference" errors by the linker. Can't a partial specialization reuse member functions of the generic template definition? If not, is there any way to avoid the great amount of copy-paste code involved in copying the generic member definitions into the partial specializations? template<int D, class F> struct L
9
1953
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... Exemple: template <class T, int Num> class A
1
2222
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
8395
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
8826
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
8605
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
5632
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
4155
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...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
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.