473,385 Members | 1,610 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,385 software developers and data experts.

partial specialized templated classes

Hi,

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

template<typename T, long CURLOPT_ID>
class CurlOption: boost::noncopyable {
public:
typedef typename boost::call_traits<T>::param_type param_type;
....
public:
// constructors:
CurlOption() {}
CurlOption(param_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_setopt(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::noncopyable
{
public:
...
template<typename T, long ID>
void setopt(const CurlOption<T, ID>& opt) {
opt.setopt(m_curl);
}

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

CurlOpt::Verbose v(on);

I get a compiler error:

EasyCurl.cpp: In member function »void EasyCurl::setVerbose(bool)«:
EasyCurl.cpp:124: Fehler: keine passende Funktion für Aufruf von
CurlOption<bool, 41l>::CurlOption(bool&)«
EasyCurl.hpp:19: Anmerkung: Kandidaten sind: CurlOption<bool,
41l>::CurlOption()
EasyCurl.hpp:19: Anmerkung: CurlOption<bool, 41l>::CurlOption(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 1822
Olaf wrote:
[..] I have the following
classes:

template<typename T, long CURLOPT_ID>
class CurlOption: boost::noncopyable {
public:
typedef typename boost::call_traits<T>::param_type param_type;
....
public:
// constructors:
CurlOption() {}
CurlOption(param_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_setopt(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<typename Tstruct OptionTraits : public boost::noncopyable { };

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

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

template<typename T, long ID>
class Option // : public boost::noncopyable
{
public:
typedef typename boost::call_traits<T>::param_type param_type;
typedef T value_type;
typedef typename boost::call_traits<T>::value_type result_type;

public:
Option(param_type 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::string, FILE_IDFile;
class Foo : boost::noncopyable
{
public:
Foo() : m_handle(0) {}

public:
void perform();

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

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

Foo foo;
foo.setopt(Verbose(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
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...
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>
7
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...
8
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 {}; ...
4
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...
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...
1
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...
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... ...
1
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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,...
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...

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.