Problem with partial specialized member functions

O

Olaf

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_VERBOSE> Verbose;

...
};

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
 
I

int19h

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.
 
O

Olaf

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
 
B

bjeremy

Olaf said:
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>());
 
O

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>());

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 T> struct 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
ID> void 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top