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.
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"
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.
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?
Looking for code to implement numpy's arange function in c++, I found this answer.
I placed the following code in a file test_arange_c.cpp:
#include <vector>
template<typename T>
std::vector<T> arange(T start, T stop, T step = 1)
{
std::vector<T> values;
for (T value = start; value < stop; value += step)
values.push_back(value);
return values;
}
int main()
{
double dt;
dt = 0.5;
auto t_array = arange<double>(0, 40, dt);
return 0;
}
When I try to compile it, I get the following error:
$ c++ test_arange_c.cpp -o test_arange_c.out
test_arange_c.cpp: In function ‘int main()’:
test_arange_c.cpp:14:8: error: ‘t_array’ does not name a type
auto t_array = arange<double>(0, 40, dt);
Without doubt, I've made a mistake that will be obvious to seasoned c++ users. But, after searching Google for a while, I haven't come up with what it is.
As #Brian suggested, I had not enabled C++11 support.
$ c++ --version
c++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
Copyright (C) 2013 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 fails:
$ c++ test_arange_c.cpp -o test_arange_c.out
test_arange_c.cpp: In function ‘int main()’:
test_arange_c.cpp:16:8: error: ‘t_array’ does not name a type
auto t_array = arange<double>(0, 40, dt);
^
This works:
$ c++ -std=c++11 test_arange_c.cpp -o test_arange_c.out
$