C++ 模板运算符专业化
我想模板化>>和您忘记使用类解析运算符:: 为其他专业也这样做C++ 模板运算符专业化,c++,templates,operators,specialization,C++,Templates,Operators,Specialization,我想模板化>>和您忘记使用类解析运算符:: 为其他专业也这样做 或者最好听从詹姆斯的建议 您忘记使用类解析运算符:: 为其他专业也这样做 或者最好听从詹姆斯的建议 您不需要专门化操作符模板:只需编写一个重载,使用std::string: 函数模板专门化很讨厌,应该尽可能避免。有关更多信息,请参阅Herb Sutter的您不需要专门化操作符模板:只需编写一个包含std::string的重载: 函数模板专门化很讨厌,应该尽可能避免。有关更多信息,请参阅Herb Sutter的我知道我看得太多了,但我
或者最好听从詹姆斯的建议 您忘记使用类解析运算符:: 为其他专业也这样做
或者最好听从詹姆斯的建议 您不需要专门化操作符模板:只需编写一个重载,使用std::string:
函数模板专门化很讨厌,应该尽可能避免。有关更多信息,请参阅Herb Sutter的您不需要专门化操作符模板:只需编写一个包含std::string的重载:
函数模板专门化很讨厌,应该尽可能避免。有关更多信息,请参阅Herb Sutter的我知道我看得太多了,但我从未想过会错过。我知道我看得太多了,但我从未想过会错过。首先,流运算符应该在类外定义!其次,如果我像在您的解决方案中那样定义运算符,我会得到以下链接器错误:LNK2005:class__cdecl运算符首先,流运算符应该在类之外定义!其次,如果我像在您的解决方案中那样定义运算符,我会得到以下链接器错误:LNK2005:class__cdecl算子
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) {
m_raw += "'";
m_raw += arg;
m_raw += "'";
return *this;
}
template<>
inline sql_command& operator>> <char*>(const char * arg) {
m_raw += "'";
m_raw += arg;
m_raw += "'";
return *this;
}
1>.\main.cpp(83) : error C2912: explicit specialization; 'sql_command &operator >><std::string>(const std::string &)' is not a specialization of a function template
1>.\main.cpp(83) : error C2805: binary 'operator >>' has too few parameters
template<>
inline sql_command& sql_command::operator>> <std::string> (const std::string& arg)
see this ^^
class sql_command {
/* ... */
template<typename T>
sql_command& operator>>(const T& arg) {
/* general purpose implementation */
}
sql_command& operator>>(const std::string& arg) {
/* special std::string implementation */
}
};