1
我跟隨this question爲了選擇合適的容器,但我遇到了問題。選擇正確的矢量與模板
我有一個selector
類必須推回到指針的向量,但正確的取決於它的尺寸(1載體,2爲矩陣):
class selector
{
struct formValues : std::vector<coolvector<double>*>, std::vector<coolmatrix<double>*> { };
formValues maps;
public:
selector() { };
template<unsigned int formdim, typename F>
void operator+=(const form<formdim, F> &f)
{
typedef typename form<formdim, F>::storage_type storage_type;
typedef typename std::vector<storage_type*> pointer_type;
// Push to the right vector
formValues<pointer_type> &m = maps;
m.push_back(f.storage.get());
}
};
的形式類具有的尺寸和存儲,也取決於尺寸,使用共享指針:
template <bool, class if_true, class if_false>
struct type_switch
{
typedef if_false type;
};
template <class if_true, class if_false>
struct type_switch<true, if_true, if_false>
{
typedef if_true type;
};
template <class T> class coolvector {};
template <class T> class coolmatrix {};
template<unsigned int formdim, typename F>
class form
{
public:
form() = delete;
form(const std::string &s) : type(s)
{
storage = std::make_shared<storage_type>();
}
std::string type;
typedef typename type_switch<formdim == 1, coolvector<double>, coolmatrix<double>>::type storage_type;
std::shared_ptr<storage_type> storage;
};
class oneform : public form<1, oneform>
{
public:
oneform() = delete;
oneform(const std::string &s) : form(s) { };
double operator()(unsigned int i) { return i * 2; };
};
class twoform : public form<2, twoform>
{
public:
twoform() = delete;
twoform(const std::string &s) : form(s) { };
double operator()(unsigned int i, unsigned int j) { return i * 2 + j * 20; };
};
的問題是,在selector::operator+=
我得到這個錯誤:
main.cpp:77:19: error: expected unqualified-id
formValues<pointer_type> &m = maps;
^
任何提示的讚賞!
'+ ='應該返回'selector *'('this')而不是'void'? ....'formValues'類型!='formValues'類型? –
javaLover
我可以返回'selector&',但它對問題沒有影響。 – senseiwa
'type_switch'看起來很像'std :: conditional'。 – aschepler