Cześć!
Mam szablon klasy kolejki parametryzowany m.in. klasą wytycznych odnośnie ochrony przed wstawieniem/zdjęciem zbyt dużej ilości elementów (checking_policy).
Wygląda to mniej-więcej tak (proszę pominąć sensowność tego rozwiązania to jest przykład ;))
#ifndef _QUEUE_H
#define _QUEUE_H
#include <list>
#include <exception>
#include <iostream>
struct ex_checking_policy {
static void check_shift(size_t current_size) {
if(current_size==0) {
std::cerr << "ERR: queue is empty" << std::endl;
throw -1;
}
}
static void check_insert(size_t current_size,size_t N) {
if(current_size >= N) {
std::cerr << "ERR: over the max possible capacity" << std::endl;
throw -1;
}
}
};
struct no_checking_policy {
static void check_insert(size_t,size_t) { };
static void check_shift(size_t) { };
};
template <typename T=int, size_t N=100, typename checking_policy = no_checking_policy>
class Queue {
private:
std::list<T> content;
public:
Queue() { }
void insert(T element)
{
try
{
checking_policy::check_insert(content.size(),N);
}
catch(...)
{
return;
}
content.push_back(element);
}
T shift()
{
try
{
checking_policy::check_shift(content.size());
}
catch(...)
{
return NULL;
}
T element = content.front();
content.pop_front();
return element;
}
bool isEmpty()
{
return content.empty();
}
};
#endif /* _QUEUE_H */
Chciałbym dodać do tej klasy możliwość kopiowania jednej kolejki do drugiej niezależnie od klasy wytycznych - czy się one różnią czy nie. Logikę tego, który ustawiać przy kopiowaniu zrobiłbym potem. Naturalnie pierwszym co mi przyszło na myśl to konstruktory kopiujące.
Dwa - wyspecjalizowane. Napisałem tak:
template<typename no_checking_policy>
Queue(const Queue<no_checking_policy> & another)
{
std::cout << "copyp no check. policy";
}
template<typename ex_checking_policy>
Queue(const Queue<ex_checking_policy> & another)
{
std::cout << "copy with checking policy";
}
Ale to nie przechodzi gdyż kompilator stwierda:
In file included from App.cpp:3:
Queue.h:52: error: â€template<class T, unsigned int N, class checking_policy> template<class ex_checking_policy="ex_checking_policy"> Queue::Queue(const Queue<no_checking_policy, 100u, no_checking_policy>&) cannot be overloaded
Queue.h:46: error: with â€template<class T, unsigned int N, class checking_policy> template<class no_checking_policy="no_checking_policy"> Queue::Queue(const Queue<no_checking_policy, 100u, no_checking_policy>&)
gmake[2]: *** [build/Debug/GNU-Linux-x86/App.o] Error 1
Czyli co, nie mogę w ten sposób przeciąrzać konstruktorów? Jeżeli nie (ok - nie znam się ja bardziej w Javie programuję :)) to w jaki inny sposób to osiągnąć?
Dzięki z góry!