带有模板函数错误的C++条件模板类型

joh*_*ull 2 c++ gcc templates boost boost-geometry

我有这样的代码

    namespace bg = boost::geometry;
    typedef typename std::conditional<highDimension,
                                    typename bg::model::point<double, 6, bg::cs::cartesian>,
                                    typename bg::model::point<double, 5, bg::cs::cartesian>>::type point;
    ..........
    point p;                    
    p.set<0>(0);
    p.set<1>(0);
    p.set<2>(0);
    ..........
Run Code Online (Sandbox Code Playgroud)

GCC向我展示了很多错误,例如"错误:类型'的无效操作数'和'int'到二进制'运算符<'p.set <1>(col.a());" 所以它只是试图'比较' p.set1

boost类实际上有模板函数集,但编译器不使用它作为函数.

如果我直接从boost类型生成typedef,就像typedef bg::model::point<double, 5, bg::cs::cartesian> point;一切正常.

我只想根据模板参数选择不同的尺寸大小highDimension.但现在我不知道如何强迫GCC了解我:)

Naw*_*waz 5

由于highDimension是模板参数,因此point成为依赖类型,因此您需要在template此处写入:

p.template set<0>(0);
Run Code Online (Sandbox Code Playgroud)

至于你为什么需要template那里,请阅读答案:


顺便说一句,你不需要typename参数:

typedef typename std::conditional<highDimension,
                 bg::model::point<double, 6, bg::cs::cartesian>,
                 bg::model::point<double, 5, bg::cs::cartesian>>::type point;
Run Code Online (Sandbox Code Playgroud)

你也可以使用using:

using point = typename std::conditional<highDimension,
                 bg::model::point<double, 6, bg::cs::cartesian>,
                 bg::model::point<double, 5, bg::cs::cartesian>>::type;
Run Code Online (Sandbox Code Playgroud)

从可读性的角度来看,这看起来更好(恕我直言).

或者你可以简单地写这个:

using point = bg::model::point<double, highDimension?6:5, bg::cs::cartesian>;
Run Code Online (Sandbox Code Playgroud)

看起来更好.