Ali Alidoust Ali Alidoust - 19 days ago 5
C++ Question

Calling a variadic template function gives error C2660 in Visual Studio 2015

The following code gives C2660 error in Visual Studio 2015 Community:

class Logger {
public:
template <class T> void writeLine(const T& value) {
if (this->filestream.is_open()) {
filestream << object << std::endl;
}
}

template <typename T> void write(const T& value) {
if (this->filestream.is_open()) {
filestream << value;
}
}

template <typename T, typename... Targs> void write(const T& value, const Targs&... args) {
if (this->filestream.is_open()) {
filestream << value;
this->write(args...);
}
}

// ... singleton stuff
}


I'm calling the function like this:

#define LOG(x) Logger::instance().write(__FILE__, " (line ", __LINE__, "): ", x, std::endl);


Here is the error in the output log:

encoder.cpp(51): error C2660: 'Logger::write': function does not take 6 arguments

Answer

The problem is that std::endl is itself a template

template< class CharT, class Traits >
std::basic_ostream<CharT, Traits>& endl( std::basic_ostream<CharT, Traits>& os );

Therefore a possible solution could be

var.write(__FILE__, " (line ", __LINE__, "): ", 22, std::endl<char, std::char_traits<char>>);

More information on this issue here