具有非类型模板参数的方法

时间:2017-05-10 13:54:24

标签: c++ templates metaprogramming non-type

我想知道创建非类型模板化类方法的正确语法是什么。我试过这个,但显然它不起作用:

<delete dir="${dir.buildfile}/doc" />
<javadoc ... />

我做错了什么?

2 个答案:

答案 0 :(得分:2)

问题是您的专业化语法不正确。它试图对一个函数进行部分特化,但这在这里甚至没有意义 - 并且无论如何都不允许。

您还尝试在第二个专门化中调用fun<B>(),但B是类型名而不是枚举值,因此无法解析调用。

请改为尝试:

// Removed template argument to make a complete specialization instead of partial.
template<>
int A::fun<A::B::C>()
{
    return 1;
}

// Removed template argument to make a complete specialization instead of partial.
template<>
int A::fun<A::B::D>()
{
    // Changed template argument from B (which is a type) to C (which is a value of
    // type B.
    return fun<C>() + 1;
}

答案 1 :(得分:1)

您正尝试部分专门化功能模板,这是不允许的。这是一个可编辑的片段:

class A
{
    enum B
    {
        C = 0,
        D
    };
    template <A::B value = A::C>
    int fun();
};

template<>
int A::fun<A::B::C>()
{
    return 1;
}

template<>
int A::fun<A::B::D>()
{
    return fun<B::C>() + 1;
}
相关问题