C++ 使用模板选择右向量

C++ 使用模板选择右向量,c++,templates,C++,Templates,为了选择正确的容器,我正在进行跟踪,但我遇到了一些问题 我有一个选择器类,它必须推回指针向量,但正确的一个取决于它的维度(1表示向量,2表示矩阵): 任何提示都将不胜感激 formValues不是模板,因此无法编写formValues 你的意思似乎是 pointer_type& m = maps; 要获取映射的相应基类子对象+=应返回选择器*(此),而不是无效表单值类型!=formValuestype?我可以返回选择器&,但它对问题没有影响。type\u开关看起来很像std::cond

为了选择正确的容器,我正在进行跟踪,但我遇到了一些问题

我有一个
选择器
类,它必须推回指针向量,但正确的一个取决于它的维度(1表示向量,2表示矩阵):


任何提示都将不胜感激

formValues
不是模板,因此无法编写
formValues

你的意思似乎是

pointer_type& m = maps;

要获取
映射的相应基类子对象

+=
应返回
选择器*
),而不是
无效
<代码>表单值类型!=
formValues
type?我可以返回
选择器&
,但它对问题没有影响。
type\u开关
看起来很像
std::conditional
。是的,它来自很远的地方!
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; };
};
main.cpp:77:19: error: expected unqualified-id
        formValues<pointer_type> &m = maps;
                  ^
pointer_type& m = maps;