模板编译限制?

时间:2014-11-04 16:54:09

标签: c++ templates static-polymorphism

这个错误很难解释,但它来自于使用静态多态性到极端。

请参阅以下代码。在代码中,如果我从一个模板化的类派生一个模板化的类,然后使用一个带有多个模板参数的模板化函数,我得到一个不太有用的编译错误

error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<' 

当派生类本身没有模板化,奇怪的是,或者模板化函数只有一个模板参数时,就不会发生这个问题。我完全难过,所以任何帮助都会非常感激。我在Fedora 7上使用gcc 4.7.2。

template <class Derived>
class BasicTemplate
{
    protected:
    template<bool param1, int param2, typename Param3>
    void templatizedFunction(const Param3 & input)
    {
    }
};

template <class C>
class DerivedTemplate : public BasicTemplate<DerivedTemplate<C> >
{
    using Base = BasicTemplate<DerivedTemplate<C> >;
    public:
    template <bool param1, int param2, typename Param3>
    void templatizedFunction(const Param3 & input)
    {
        Base::templatizedFunction<param1, param2>(input);
    }
    C myData_;
};

class DerivedNonTemplate : public BasicTemplate<DerivedNonTemplate>
{
    using Base = BasicTemplate<DerivedNonTemplate>;
    public:
    template <bool param1, int param2, typename Param3>
    void templatizedFunction(const Param3 & input)
    {
        Base::templatizedFunction<param1, param2>(input);
    }
};

int main()
{
    DerivedTemplate<int> derivedTemplate;
    DerivedNonTemplate derivedNonTemplate;
    derivedTemplate.templatizedFunction<false, 1>(1);
    derivedNonTemplate.templatizedFunction<false, 1>(1);
    return 0;
}

0 个答案:

没有答案