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?
Related
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.
Consider the following program:
#include <stdexcept>
#include <stdio.h>
#include <memory>
#include <list>
class Foo {
public:
Foo(){
if (s_ct==0) {throw std::bad_alloc();}
--s_ct;
fprintf(stderr, "ctor %p\n", this);
}
~Foo(){
fprintf(stderr, "dtor %p\n", this);
}
private:
static int s_ct;
};
int Foo::s_ct = 2;
int main(){
try {
std::list<std::shared_ptr<Foo>> l = {
std::make_shared<Foo>(),
std::make_shared<Foo>(),
std::make_shared<Foo>()
};
} catch (std::bad_alloc&) {
fprintf(stderr, "caught exception.\n");
}
fprintf(stderr, "done.\n");
return 0;
}
Compiled like this:
[little:~] $ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
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.
[little:~] $ g++ --std=c++14 -o list_init list_init.cc
[little:~] $
The output is:
[little:~] $ ./list_init
ctor 0x1294c30
ctor 0x1294c50
caught exception.
done.
[little:~] $
Notice that the destructors are not called. Valgrind correctly complains of the leak as well.
This seems to violate one of the key purposes of std::make_shared -- namely, that if another expression in the statement throws, the shared object gets properly destroyed because it is wrapped by a fully constructed shared pointer object.
Clang does what I would like here:
[little:~] $ clang++ --version
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
[little:~] $ clang++ --std=c++14 -o foo list_init.cc
[little:~] $ ./foo
ctor 0x1dfec30
ctor 0x1dfec50
dtor 0x1dfec50
dtor 0x1dfec30
caught exception.
done.
[little:~] $
So is this a GCC bug, or do I need to fix my program?
Turning my comment into an answer so you can mark the question as answered.
This looks to be a gcc bug
Bug 66139 - destructor not called for members of partially constructed anonymous struct/array
Note in particular the last two test cases which use std::initializer_list to illustrate the problem.
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 understand that if a temporary is bound to a reference member in the constructor's initializer list, the object will be destroyed as the constructor returns.
However, consider the following code:
#include <functional>
#include <iostream>
using callback_func = std::function<int(void)>;
int
func(const callback_func& callback)
{
struct wrapper
{
const callback_func& w_cb;
wrapper(const callback_func& cb) : w_cb {cb} { }
int call() { return this->w_cb() + this->w_cb(); }
};
wrapper wrp {callback};
return wrp.call();
}
int
main()
{
std::cout << func([](){ return 21; }) << std::endl;
return 0;
}
This looks perfectly valid to me. The callback object will live during the whole execution of the func function and no temporary copy should be made for wrapper's constructor.
Indeed, GCC 4.9.0 compiles fine with all warnings enabled.
However, GCC 4.8.2 compiler gives me the following warning:
$ g++ -std=c++11 -W main.cpp
main.cpp: In constructor ‘func(const callback_func&)::wrapper::wrapper(const callback_func&)’:
main.cpp:12:48: warning: a temporary bound to ‘func(const callback_func&)::wrapper::w_cb’ only persists until the constructor exits [-Wextra]
wrapper(const callback_func& cb) : w_cb {cb} { }
^
Is this a false positive or am I misunderstanding the object lifetimes?
Here are my exact compiler versions tested:
$ g++ --version
g++ (GCC) 4.8.2
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.
$ g++ --version
g++ (GCC) 4.9.0 20140604 (prerelease)
Copyright (C) 2014 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 is a bug in gcc 4.8 that has been fixed in 4.9. Here is the bug report:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50025
As pointed out by Howard Hinnant and already indicated by R Sahu's comment, this is a bug (which used to be required by the then-broken standard; thanks to Tony D for pointing this out) in the way GCC 4.8 is treating initializer lists.
Changing the constructor in my original example from
wrapper(const callback_func& cb) : w_cb {cb} { }
to
wrapper(const callback_func& cb) : w_cb (cb) { }
makes the warning with GCC 4.8.3 go away and the created executable Valgrind clean. The diff of the two assembly files is huge so I don't post it here. GCC 4.9.0 creates identical assembly code for both versions.
Next, I replaced the std::function with a user-defined struct and deleted copy and move constructors and assignment operators. Indeed, with GCC 4.8.3, this retains the warning but now also gives a (slightly more helpful) error that the above line of code calls the deleted copy constructor of the struct. As expected, there is no difference with GCC 4.9.0.
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"}};
}