1

I'm compiling this on macOS using clang/llvm 8.0.0. Compiling for C++14.

#include <fstream>
#include <string>

using namespace std;

basic_fstream<string> open_file(string file);

int main()
{
    basic_fstream<string> f = open_file("test");
    f.close();
}

basic_fstream<string> open_file(string file) {
    basic_fstream<string> f;
    f.open(file);

    if(!f.is_open()) {
        f.clear();
        f.open(file, ios_base::out);
        f.close();
        f.open(file);
    }

    return f;
}

It produces a long error list but the first is:

implicit instantiation of undefined template 'std::__1::codecvt<std::__1::basic_string<char>, char, __mbstate_t>'
__always_noconv_ = __cv_->always_noconv();
Brady Dean
  • 3,378
  • 5
  • 24
  • 50

1 Answers1

1

The template parameter to basic_fstream is a character type, not a string type. The typical character types that are supported by a C++ implementation would be basic_fstream<char> and basic_fstream<wchar_t>, for example.

But why use this template? Just use std::fstream, a.k.a. std::basic_fstream<char>; or std::wfstream, a.k.a. std::basic_fstream<wchar_t>.

It also wouldn't hurt to get rid of using namespace std; while your at it, too.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Ok that makes sense. Are char and wchar the only charT's defined, and what are the requirements to be a charT? And don't worry, using namespace std was only for the example – Brady Dean Oct 05 '16 at 01:28
  • The C++ standard specifies only `char` and `wchar_t`, but a given C++ implementation is permitted to support other character types, of course. – Sam Varshavchik Oct 05 '16 at 01:32
  • Ok so it's not a thing you can make a class of yourself, it would have to implemented beforehand? – Brady Dean Oct 05 '16 at 01:34
  • It will have to be implemented by your C++ library. – Sam Varshavchik Oct 05 '16 at 01:36
  • 1
    @SamVarshavchik: C++11 added new standard `char16_t` and `char32_t` character types, to help people get away from the portability issues of `wchar_t`. Whether a given library implements STL classes that support those new types is another issue. – Remy Lebeau Oct 05 '16 at 02:38