Reputation: 374
Let's say we have a template class with a nested class inside it. The nested class has a template member function. It seems that I managed to declare a specialized version of the member function but cannot define it outside of the class.
Here is an example :
template<class Stream>
class Logger
{
class LogEntry
{
public:
LogEntry(Logger<Stream>& logger, Stream& stream)
: m_stream(stream)
{
}
LogEntry(const LogEntry&) = delete;
LogEntry(LogEntry&&) noexcept = default;
template<typename Loggable>
LogEntry& operator<< (const Loggable& loggable);
template<>
LogEntry& operator<< <int>(const int& loggable);
private:
Stream& m_stream;
};
public:
Logger(Stream& stream) : m_stream(stream) {}
LogEntry operator<<(const auto& loggable)
{
LogEntry entry(*this, m_stream);
entry << loggable;
return entry;
}
private:
Stream& m_stream;
};
// Definition of the previously declared member function (operator)
template<class Stream>
template<typename Loggable>
Logger<Stream>::LogEntry& Logger<Stream>::LogEntry::operator<<(const Loggable& loggable)
{
m_stream << loggable;
return *this;
}
// Failing to define specialized version of the function that is successfully declared in the class. With error : "cannot specialize (with 'template<>') a member of an unspecialized template"
template<class Stream>
template<>
Logger<Stream>::LogEntry& Logger<Stream>::LogEntry::operator<<<int>(const int& loggable)
{
m_stream << loggable;
return *this;
}
Any idea on what I'm doing wrong in this example?
Thanks in advance
Upvotes: 0
Views: 15