简体   繁体   中英

Template specialization of a member function

I have to use an explicit specialization for a class member function in (C++ , I am using MS Visual Studio 2008 SP1), but I could not success to compile it. Getting

error C2910: 'File::operator <<' : cannot be explicitly specialized

class File
{
   std::ofstream mOutPutFile;
public:
   template<typename T>
   File& operator<<(T const& data);
};


template<typename T>
File& File::operator<< (T const& data)
{
    mOutPutFile << preprosesor(data);
    return *this;
}

template< >
File& File::operator<< <> (std::ofstream& out)
{
   mOutPutFile << out;
   return *this;
}

Your explicit specialization of operator << didn't match the parameter list of the declaration; T const& data vs std::ofstream& out . This one compiles in MSVC10.

template<>
File& File::operator<< <std::ofstream> (const std::ofstream& out)
  {
  mOutPutFile << out;
  return *this;
  }

Notice const added before the function parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM