如何在c ++类中使用模板专门化,为什么这不编译? [英] How can I use template specialization in c++ classes, and why this doesn't compile?

查看:227
本文介绍了如何在c ++类中使用模板专门化,为什么这不编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 XmlWriter 类,我想能够以大多数标准数据格式(字符串,整数,浮点数等)输出属性或文本, 。为此,我使用了一个文件流。



对于 bool 数据类型,专用化模板,使其输出 true false ,而不是 1

>

  class XmlWriter {

private:/ * ... * /

:/ * ... * /

template< typename T>
void writeText(T text){
/ * ... * /
}

template<> //< - error:explicit specialization in non-namespace scope'class Strategy :: IO :: XmlWriter'
void writeText< bool> (bool text){//< - error:template-id'writeText<>在主模板的声明中
/ * ... * /
}
$ b b模板< typename T>
void writeAttribute(std :: string key,T value){//< - 错误:模板参数列表过多
/ * ... * /
}

模板<> //< - error:explicit specialization in non-namespace scope'class Strategy :: IO :: XmlWriter'
void writeAttribute< bool> (std :: string key,bool value){//< - 错误:变量或字段'writeAttribute'声明void; expected';'before'<'token
/ * ... * /
}
}; //< - expected';'before'} token



我不明白,为什么所有这些错误,因为我使用在互联网上的各种网站上提供的正确语法?



我使用Cygwin GCC。

解决方案


在非命名空间范围中的显式专门化'class Strategy :: IO :: XmlWriter'




尝试将专业化转移到命名空间范围?

  class XmlWriter {

private:/ * ... * /

public:/ * ... * /

template< typename T>
void writeText(T text){
}


template< typename T>
void writeAttribute(std :: string key,T value){
}


};

模板<>
void XmlWriter :: writeText< bool> (bool text){
}

模板<>
void XmlWriter :: writeAttribute< bool> (std :: string key,bool value){
}


I am working on a XmlWriter class, and I wanted to be able to output attributes or text in most standard data formats (strings, integers, floating point numbers etc). To achieve this, I am using a file stream.

For the bool data type, I wanted to specify a specialization to the template, so that it outputs true and false instead of 1 and 0.

However, the following code doesn't seem to compile:

class XmlWriter {

private: /* ... */

public: /* ... */

    template <typename T>
    void writeText(T text)  {
        /* ... */
    }

    template <>  // <-- error: explicit specialization in non-namespace scope 'class Strategy::IO::XmlWriter'
    void writeText<bool> (bool text) {  // <-- error: template-id 'writeText<>' in declaration of primary template
        /* ... */
    }

    template <typename T> 
    void writeAttribute(std::string key, T value) { // <-- error: too many template-parameter-lists
        /* ... */
    }

    template <>  // <-- error: explicit specialization in non-namespace scope 'class Strategy::IO::XmlWriter'
    void writeAttribute<bool> (std::string key, bool value) { // <-- error: variable or field 'writeAttribute' declared void; expected ';' before '<' token
        /* ... */
    }
}; // <-- expected ';' before '}' token

I don't understand, why all these errors, since I used the correct syntax presented on various websites on the internet?

I am using Cygwin GCC.

解决方案

explicit specialization in non-namespace scope 'class Strategy::IO::XmlWriter'

Try moving the specialization into namespace scope?

class XmlWriter {

private: /* ... */

public: /* ... */

    template <typename T>
    void writeText(T text)  {
    }


    template <typename T>
    void writeAttribute(std::string key, T value) {
    }


}; 

template <>
void XmlWriter::writeText<bool> (bool text) {
}

template <>
void XmlWriter::writeAttribute<bool> (std::string key, bool value) {
}

这篇关于如何在c ++类中使用模板专门化,为什么这不编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆