Skip to content

Instantly share code, notes, and snippets.

@fowlmouth
Created May 29, 2022 17:56
Show Gist options
  • Save fowlmouth/3206cc1e6b866e59a173e962163a52ed to your computer and use it in GitHub Desktop.
Save fowlmouth/3206cc1e6b866e59a173e962163a52ed to your computer and use it in GitHub Desktop.
streams issue
#!/bin/sh
c++ main.cc -o main -std=c++20 && ./main
#include <sstream>
#include <iostream>
#include <locale>
#include <codecvt>
template< typename stream >
void test(stream&& ss)
{
// stream ss("abc");
for(int i = 0; i < 4; ++i)
{
std::cout << (int)ss.get() << ' ';
}
std::cout << std::endl;
ss.clear();
ss.seekg(0, ss.beg);
for(int i = 0; i < 4; ++i)
{
std::cout << (int)ss.get() << ' ';
}
std::cout << std::endl;
}
int main()
{
test< std::istringstream >(std::istringstream{"abc"});
std::cout << "---" << std::endl;
test< std::wistringstream >(std::wistringstream{L"abc"});
std::cout << "---" << std::endl;
test< std::wistream >(std::wistringstream{L"abc"});
std::cout << "---" << std::endl;
{
std::stringbuf buf;
buf.str("abc");
std::wbuffer_convert< std::codecvt_utf8< wchar_t >> conv(&buf);
test< std::wistream >(std::wistream(&conv));
}
return 0;
}
97 98 99 -1
97 98 99 -1
---
97 98 99 -1
97 98 99 -1
---
97 98 99 -1
97 98 99 -1
---
97 98 99 -1
-1 -1 -1 -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment