类方法定义后的实现

时间:2014-05-08 12:49:44

标签: c++ templates c++11

这不是确切的情况,但它非常相似,完全适用于我的情况:

我有一个班级

template<typename T, std::size_t Size>
class MahClass{
    public:
        template<std::size_t S = Size, typename = typename std::enable_if<...>::type>
        operator other_class<T, 1, Size>();
};

// Of course the `...` is substituted for my condition

我想在类之外实现该转换运算符,但我不知道为此所需的语法。

我试过了:

template<typename T, std::size_t Size>
MahClass::operator<> other_class<T, 1, Size>(){...}

但我知道这不对, 是什么?

2 个答案:

答案 0 :(得分:2)

可以用以下方式编写:

template <typename T, std::size_t Size>
template <std::size_t S, typename> // <--
MahClass<T, Size>::operator other_class<T, 1, Size>()
//      ^^^^^^^^^
{
    // ...
}

答案 1 :(得分:1)

您需要做的就是声明类和方法模板参数列表。该课程首先是方法,然后是方法。像这样:

template<typename T, size_t Size>
template<std::size_t S, typename>
Yolo<T, Size>::operator other_class<T, 1, Size>() { }