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

Problem with partial specialized member functions

Hi,

I'm writing a small wrapper for libcurl. For Options I use:

template<typename T, long CURLOPT_ID>
class CurlOption: boost::noncopyable
{
public:
typedef typename boost::call_traits<T>::param_type param_type;
typedef typename boost::call_traits<T>::reference reference;
typedef typename boost::call_traits<T>::const_reference
const_reference;
typedef T value_type;
typedef typename boost::call_traits<T>::value_type
result_type;

public:
CurlOption() {}
CurlOption(param_type v) : m_value(v) {}

long option() const { return CURLOPT_ID; }
result_type parameter() { return m_value; }
reference get() { return m_value; }
const_reference get() const { return m_value; }

private:
value_type m_value;
};

and than typedef

class CurlOpt {
typedef CurlOption<bool, CURLOPT_VERBOSEVerbose;

...
};

and further more:

class EasyCurl : boost::noncopyable {
public:
template<typename T, long ID>
void setopt(const CurlOption<T, ID>& opt); //L351

};

and partially specialize the member function

template<long ID>
inline
void EasyCurl::setopt(const CurlOption<bool, ID>& opt) { //L389
curl_easy_setopt(m_curl, opt.option(), opt.value() ? 1 : 0);
}

The try to compile results in the error:
EasyCurl.hpp:389: Fehler: Prototyp für »void
pEDA::EasyCurl::setopt(const pEDA::CurlOption<bool, ID>&)« passt zu
nichts in Klasse »pEDA::EasyCurl«
EasyCurl.hpp:351: Fehler: Kandidat ist: template<class T, long int ID>
void pEDA::EasyCurl::setopt(const pEDA::CurlOption<T, ID>&)
EasyCurl.hpp:389: Fehler: Template-Definition eines Nicht-Templates
»void pEDA::EasyCurl::setopt(const pEDA::CurlOption<bool, ID>&)«

I'm wrong, obviously. How can I get it work?

Thanks,
Olaf
Jul 22 '07 #1
4 1786
On Jul 22, 11:13 pm, Olaf <o...@mdcc.dewrote:
class EasyCurl : boost::noncopyable {
public:
template<typename T, long ID>
void setopt(const CurlOption<T, ID>& opt); //L351

};

and partially specialize the member function

template<long ID>
inline
void EasyCurl::setopt(const CurlOption<bool, ID>& opt) { //L389
curl_easy_setopt(m_curl, opt.option(), opt.value() ? 1 : 0);
}

The try to compile results in the error:
The problem is that C++ simply does not allow partial specialization
of functions, but only of classes. The common workaround is to move
the code from the function into a helper template class (so that the
function just delegates any calls to that class), and then specialize
the class.

Jul 23 '07 #2
in****@gmail.com schrieb:
On Jul 22, 11:13 pm, Olaf <o...@mdcc.dewrote:
> class EasyCurl : boost::noncopyable {
public:
template<typename T, long ID>
void setopt(const CurlOption<T, ID>& opt); //L351

};

and partially specialize the member function

template<long ID>
inline
void EasyCurl::setopt(const CurlOption<bool, ID>& opt) { //L389
curl_easy_setopt(m_curl, opt.option(), opt.value() ? 1 : 0);
}

The try to compile results in the error:

The problem is that C++ simply does not allow partial specialization
of functions, but only of classes. The common workaround is to move
the code from the function into a helper template class (so that the
function just delegates any calls to that class), and then specialize
the class.
Do you have an examples of this?

Thanks
Olaf
Jul 23 '07 #3

Olaf wrote:
in****@gmail.com schrieb:
On Jul 22, 11:13 pm, Olaf <o...@mdcc.dewrote:
class EasyCurl : boost::noncopyable {
public:
template<typename T, long ID>
void setopt(const CurlOption<T, ID>& opt); //L351

};

and partially specialize the member function

template<long ID>
inline
void EasyCurl::setopt(const CurlOption<bool, ID>& opt) { //L389
curl_easy_setopt(m_curl, opt.option(), opt.value() ? 1 : 0);
}

The try to compile results in the error:
The problem is that C++ simply does not allow partial specialization
of functions, but only of classes. The common workaround is to move
the code from the function into a helper template class (so that the
function just delegates any calls to that class), and then specialize
the class.

Do you have an examples of this?

Thanks
Olaf
Usually I like to look at my references since my long term memory is
swiss cheese.. but

//Helper Template class
template <typename T>
struct Type2Type
{
typedef T OrigType;
};

//Generic Implementation
template <class U, class T>
T* Foo(const U& arg, Type2Type<T>)
{
return new T(arg);
}

//Partial Specialization
template <class U>
Bar* Foo(const U& arg, Type2Type<Bar>)
{
return new Bar(arg);
}

// Usage
String* pStr = Foo("Hello", Type2Type<String>());
Bar* pBar = Foo(1, Type2Type<Bar>());

Jul 23 '07 #4
Usually I like to look at my references since my long term memory is
swiss cheese.. but

//Helper Template class
template <typename T>
struct Type2Type
{
typedef T OrigType;
};

//Generic Implementation
template <class U, class T>
T* Foo(const U& arg, Type2Type<T>)
{
return new T(arg);
}

//Partial Specialization
template <class U>
Bar* Foo(const U& arg, Type2Type<Bar>)
{
return new Bar(arg);
}

// Usage
String* pStr = Foo("Hello", Type2Type<String>());
Bar* pBar = Foo(1, Type2Type<Bar>());
Thank you; did I right understand you? Unfortunately my code doesn't
compile:

typedef CurlOption<bool, CURLOPT_VERBOSE> Verbose;

class EasyCurl : boost::noncopyable {
private:
template<typename Tstruct dispatch { };

template<typename T, long ID// L342, below L343
void setopt(const CurlOption<T, ID>& opt, dispatch<T>);
[..]
public:
template<typename T, long ID>
void setopt(const CurlOption<T, ID>& opt) { // L357
setopt(opt, dispatch<T>());
}
};

template<typename T, long ID>
inline // L394, below L395
void EasyCurl::setopt(const CurlOption<bool, ID>& opt, dispatch<bool>) {
curl_easy_setopt(m_curl, opt.option(), opt.value() ? 1 : 0);
}

The errors are:

EasyCurl.hpp:395: Fehler: Prototyp für »void EasyCurl::setopt(const
CurlOption<bool, ID>&, EasyCurl::dispatch<bool>)« passt zu nichts in
Klasse »EasyCurl«
EasyCurl.hpp:357: Fehler: Kandidaten sind: template<class T, long int
IDvoid EasyCurl::setopt(const CurlOption<T, ID>&)
EasyCurl.hpp:343: Fehler: template<class T, long int ID>
void EasyCurl::setopt(const CurlOption<T, ID>&, EasyCurl::dispatch<T>)
EasyCurl.hpp:395: Fehler: Template-Definition eines Nicht-Templates
»void EasyCurl::setopt(const CurlOption<bool, ID>&,
EasyCurl::dispatch<bool>)«
Thanks,
Olaf
Jul 23 '07 #5

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

Similar topics

1
by: SainTiss | last post by:
Hi, I've been looking into the standard for a clear statement on whether partial specialization of member functions of class templates is allowed or not. 14.7.3/4 says that explicit...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
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...
1
by: Nathan Addy | last post by:
I am trying to partially specialize a template in the following way: I have two template classes defined: template <typename T> class Foo { public: Foo(); }; template <typename T>
2
by: shuisheng | last post by:
Dear All, Assume I have a class template: template<class T, int N = 0> class A { void fun1(); void fun2(); void fun3();
16
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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...
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.