【发布时间】:2017-04-03 13:16:58
【问题描述】:
我关注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<pointer_type>类型? -
我可以返回
selector&,但对问题没有影响。 -
type_switch看起来很像std::conditional。 -
是的,它来自很远的地方!