By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,039 Members | 1,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,039 IT Pros & Developers. It's quick & easy.

Problem with partial specialized member functions

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.