Is std::basic_ifstream supposed to work with std::byte as the CharT?
The following snippet.cpp godbolt:
#include <fstream>
#include <sstream>
#include <cassert>
auto main() -> int {
std::basic_string<std::byte> data { std::byte{0x12}, std::byte{0x13} };
std::basic_ofstream<std::byte>("path") << data;
std::basic_stringstream<std::byte> ss {};
ss << std::basic_ifstream<std::byte>("path").rdbuf();
assert(data == ss.str());
}
fails the assertion. The file is created, but it's empty.
How am I supposed to read/write std::basic_string<std::byte> to the filesystem?
$ g++ --std=c++17 snippet.cpp && ./a.out
a.out: code.cpp:10: int main(): Assertion `data == ss.str()' failed.
$ g++ --version
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Related
The code
#include <cstdint>
#include <cstring>
int main()
{
int len = 10;
uint8_t data[len] = {};
uint8_t src[len] = { 1, 2, 3 };
std::memcpy(data, src, len);
return 0;
}
build with g++-12
➜ test g++ -Wall test.cpp
test.cpp: In function ‘int main()’:
test.cpp:7:30: warning: value computed is not used [-Wunused-value]
7 | uint8_t data[len] = {};
| ^
test.cpp:8:38: warning: value computed is not used [-Wunused-value]
8 | uint8_t src[len] = { 1, 2, 3 };
| ^
➜ test g++ --version
g++ (Ubuntu 12-20220319-1ubuntu1) 12.0.1 20220319 (experimental) [master r12-7719-g8ca61ad148f]
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
build with g++-11
➜ test g++-11 -Wall test.cpp # no errors, no warnings
➜ test g++-11 --version
g++-11 (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The data and src is used, but why did the g++-12 report an "unused value" warning, and how should I fix the code so that g++-12 doesn't report "value computed is not used"
I have the following code to copy one raw (not null terminated) string into const-size char array, but when I compile I get a warning. Could please someone help me to understand what's wrong with this peace of code?
#include <array>
#include <cstring>
char reason_[20];
template <size_t N>
void CopyStr(char(&out)[N], const std::string_view& in)
{
memcpy(out, in.data(), std::min(in.size(), N));
if (in.size() < N)
out[in.size()] = '\0';
}
int main()
{
CopyStr(reason_, "User request");
}
g++-8 -O3 -mavx -m64 -std=c++17 -g0 -DNDEBUG -fconcepts -Wall test.cpp
In function ‘void* memcpy(void*, const void*, size_t)’,
inlined from ‘void CopyStr(char (&)[N], const string_view&) [with long unsigned int N = 20]’ at test.cpp:9:11,
inlined from ‘int main()’ at test.cpp:16:12:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:34:33: warning: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ forming offset [14, 20] is out of the bounds [0, 13] [-Warray-bounds]
return __builtin___memcpy_chk (__dest, __src, __len, __bos0 (__dest));
The following is output of g++-8 --version
g++ (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
No warnings for
clang version 9.0.0-2 (tags/RELEASE_900/final)
Target: x86_64-pc-linux-gnu
No warnings for
g++ (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
thread_local is defined in C++11 to have dynamic initialization semantics, so that it is permissible to declare non-POD types as thread local. However, in this program, I get an unexpected result:
#include <iostream>
struct A {
A() : repr_(0) {}
A(int) : repr_(2) {}
int repr_;
};
thread_local A x(2);
int main() {
std::cerr << x.repr_ << "\n";
return 0;
}
On GCC 4.8 I get:
$ g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -std=c++11 test.cpp && ./a.out
0
The program works correctly if I replace the thread_local line with:
thread_local A x = A(2);
What's going on here?
I'm trying to have a function return std::tuple<Qstring, int>, but I'm getting this compiler error:
std::tuple<QString, int> foo()
{
auto fst = getFst();
auto snd = getSnd();
return std::make_tuple(fst, snd);
}
`error: no viable conversion from 'tuple<[...], typename __make_tuple_return::type>' to 'tuple<[...], int>'``
What am I doing wrong?
There's nothing wrong with this code. It compiles without any issues.
$ cat t.C
#include <QString>
#include <tuple>
std::tuple<QString, int> foo()
{
QString fst = QString("fst");
int snd = 2;
return std::make_tuple(fst, snd);
}
$ g++ -std=c++11 -I/usr/include/QtCore -c -o t.o t.C
$ g++ --version
g++ (GCC) 5.3.1 20151207 (Red Hat 5.3.1-2)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
i'm in a situation with a declaration of vector<vector<string>>. On windows it's ok i can declare this in a struct like vector<vector<string>>v={{"me","you"}} but on a linux machine..only errors so i must declare it after the struct initialization but how because mystruct.vec[0]={"me","you"} gives me a segmentation fault. Any sugestions please?
This program on gcc 4.7.2 works just fine:
#include <vector>
#include <string>
#include <utility>
#include <iostream>
using ::std::vector;
using ::std::string;
using ::std::move;
vector<vector<string>> foo()
{
vector<vector<string>>v={{"me","you"}};
return move(v);
}
int main()
{
using ::std::cout;
cout << "{\n";
for (auto &i: foo()) {
cout << " {\n";
for (auto &o: i) {
cout << " \"" << o << "\",\n";
}
cout << " },\n";
}
cout << "}\n";
return 0;
}
It produces this output:
$ /tmp/a.out
{
{
"me",
"you",
},
}
I think your problem is either an old compiler or that you have some other problem in some other place in your code.
I used this command line to compile:
$ g++ -std=gnu++0x -march=native -mtune=native -Ofast -Wall -Wextra vvstr.cpp
And my g++ gives this as a version:
$ g++ --version
g++ (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8)
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
This page tells you which version of gcc has which C++ feature:
http://gcc.gnu.org/projects/cxx0x.html
If you are using GCC, them you need a version that supports this C++11 initialization feature, and then you need to tell the compiler to compile in C++11 mode by passing it the -std=c++0x flag (or =std=c++11 for the 4.7 series). See this demo, compiled with GCC 4.7.2:
#include <vector>
#include <string>
int main()
{
std::vector<std::vector<std::string>> v = {{"me","you"}};
}