小编Kha*_*sar的帖子

特征和传递特征作为模板参数

什么时候将traits作为模板参数传递,而不是简单地使用一些现有的traits结构

typedef basic_ofstream< char, char_traits<char> >

typedef basic_ofstream< char >

我有一些瓷砖类,我希望有一些共同点(特征),所以我设计tile_traits包含有关瓷砖的所有基本信息,例如int_typeflag_type,像这样:

//unspecialized
template<typename T> struct tile_traits;
//... other stuff here, declaration of a tile class
template<>
struct tile_traits<tile_class>
{
   typedef tile_class::int_type  int_type;
   typedef tile_class::flag_type flag_type;
   //other possible tile info here.
}
Run Code Online (Sandbox Code Playgroud)

设计特征是否被视为特征 - 特征

c++ templates idioms traits

7
推荐指数
2
解决办法
2547
查看次数

模板<<和>>运算符专门化

我想在类中模拟>><<运算符,但我也想将它们专门用于字符串,所以我这样做了;

    class sql_command
{
public:
    sql_command() { }

    explicit sql_command(std::string& cmd)
        : m_raw(cmd) {
    }

    inline sql_command& operator<<(const char * arg)
    {
        m_raw += arg;
        return *this;
    }

    inline sql_command& operator<<(const std::string& arg)
    {
        m_raw += arg;
        return *this;
    }

    template<typename T>
    inline sql_command& operator>>(const T arg)
    {
        m_raw += boost::lexical_cast<std::string>(arg);
        return *this;
    }

    inline std::string const& command() const {
        return m_raw;
    }

private:
    std::string m_raw;
};

template<>
inline sql_command& operator>> <std::string> (const std::string& arg) { …
Run Code Online (Sandbox Code Playgroud)

c++ templates operators specialization

1
推荐指数
1
解决办法
5986
查看次数

标签 统计

c++ ×2

templates ×2

idioms ×1

operators ×1

specialization ×1

traits ×1