C++ - 模板类中模板函数的单独声明/定义

Bra*_*ler 7 c++ oop methods implementation templates

我知道在头文件中声明模板类方法并在源文件中定义它的语法如下:

myclass.h

template <typename T>
class MyClass {
  public:
    void method(T input);
  private:
    T privVar;
};
Run Code Online (Sandbox Code Playgroud)

myclass.cpp

template <typename T>
void MyClass<T>::method(T input) {
    privVar = input;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果该方法也是模板怎么办?我正在为basic_string类添加方法,我想知道如何编写函数的实现.

MyString.h

template <class _Elem   = TCHAR,
          class _Traits = std::char_traits<_Elem>,
          class _Ax     = std::allocator<_Elem>>
class String
    : public std::basic_string<_Elem, _Traits, _Ax> {
  private:
    // Types for the conversion operators.
    typedef       _Elem* _StrTy;
    typedef const _Elem* _ConstStrTy;

    //...

  public:
        // Conversion operators so 'String' can easily be
        // assigned to a C-String without calling 'c_str()'.
    operator _StrTy() const {
        return const_cast<_StrTy>(this->c_str());
    }

    operator _ConstStrTy() const {
        return this->c_str();
    }

    // ... Constructors ...

    /*------------ Additional Methods ------------*/

    //! Converts a value of the given type to a string.
    template <class _ValTy> static String ConvertFrom(_ValTy val);

    //! Converts a string to the given type.
    template <class _ValTy> static _ValTy ConvertTo(const String& str);
    template <class _ValTy> _ValTy ConvertTo(void) const;

    //! Checks if a string is empty or is whitespace.
    static bool IsNullOrSpace(const String& str);
    bool IsNullOrSpace(void) const;

    //! Converts a string to all upper-case.
    static String ToUpper(String str);
    void ToUpper(void);

    // ...
};
Run Code Online (Sandbox Code Playgroud)

我该怎么template <class _ValTy> static String ConvertFrom(_ValTy val);办?因为现在我不仅需要指定类模板,还需要指定函数模板.我打赌我要写的代码无效,但它应该显示我想要完成的事情:

MyString.cpp

template <class _Elem, class _Traits, class _Ax>
template <class _ValTy>
String<_Elem, _Traits, _Ax> String<_Elem, _Traits, _Ax>::ConvertFrom(_ValTy val) {
    // Convert value to String and return it...
}
Run Code Online (Sandbox Code Playgroud)

我对模板一点也不高级.我不仅非常怀疑上述内容是否有效,编写起来似乎很麻烦而且不太可读.我将如何实现模板方法,以及返回其自己的类类型的静态模板方法?因为我不想在标题中定义它们.

Ros*_*ost 16

模板外部模板成员函数定义的语法如下:

template <class T> struct A
{
   template <class X> void f();
};

template<class T> template<class X> void A<T>::f()
{
}
Run Code Online (Sandbox Code Playgroud)

所以你的代码是正确的.

需要注意的是,定义模板成员.cpp并不是很有用.在这种情况下,您应使用此模板使用所有类型显式实例化它们.或者不要在没有意义的情况下使用它们.cpp.


Mar*_*k B 10

在我回答你的问题之前,我先说:不要这样做.std::string通过使用自由函数进行扩展,就像标准库实现许多算法一样.此外,我建议为范围而不是string仅限范围进行,但这更主观.

还要注意的是std::string避免了隐式转换为C字符串不要打乱你的生活,而是为了保护您的代码从各种晦涩的错误,可以由意外的隐式转换而引起的.我认为实施它们需要很长时间.试想想:这需要你一些额外的时间来输入.c_str(),当你写的代码一次,并为永恒的人的休息谁读你的代码会立即知道,它被用来作为一个C风格的字符串,而不是一个std::string.

要回答您的问题,只需将代码放在标题中:

//! Converts a value of the given type to a string.
template <class _ValTy> static String ConvertFrom(_ValTy val)
{
    // Code here
}
Run Code Online (Sandbox Code Playgroud)

最后请注意,开始下划线+大写字母(和许多开始与其他事物标识符_)保留给编译器,因此所有的赌注都关闭,以你的程序的功能.