c++fileprintingwofstream

Print to file via wofstream


Below code does not work and give the compilation error of:

Severity Code Description Project File Line Suppression State Error invalid operands to binary expression ('std::wofstream' (aka 'basic_ofstream >'))

And the code is:

template <class T>
void printtofile(std::string filename, int mode, T var, std::wstring msg)
{
    wofstream outfile;
    if (mode == 0) outfile.open(filename); else outfile.open(filename, ios::out | ios::app);
    outfile << msg << L"\n";
    outfile << var << L"\n";
    outfile.close();
}

If I comment out the below line there is no error.

    outfile << var << L"\n";

Okay. The weird and confusing thing is that If I add the function with different parameters as the following there is no error although I do not comment out the line I mentioned above:

template <class T>
void printtofile(std::string filename, int mode, T var)
{
    wofstream outfile;
    if (mode == 0) outfile.open(filename); else outfile.open(filename, ios::out | ios::app);
    outfile << var << L"\n";
    outfile.close();
}

Isn't this the same thing? What is going on here?


Solution

  • If outfile << var << L"\n"; fails to compile, it's because var is of a type that doesn't have an overload like:

    std::wofstream& operator<<(std::wofstream&, const T&);
    

    If you for example try to pass a std::string to the function, that would cause the same error.

    To make the error clearer for a user of the function, you can use SFINAE to only instantiate the template for supported types:

    template<class T>
    auto printtofile(const std::string& filename, int mode, T var,
                     const std::wstring& msg)
        -> decltype(std::wofstream{} << var, void()) {
    //...
    }
    

    If you now try to use it with an unsupported type, the error will become something like:

    error: no matching function for call to
     ‘printtofile(..., int, <the offending type>, std::wstring&)’