Weird (unintuitive?) behavior of std::tuple
Tomasz Gajewski
tomga@wp.pl
Sat Oct 30 13:43:00 GMT 2010
Alex Dubov <oakad@yahoo.com> writes:
> In the primitive example, attached below, constructors for B and C
> will be invoked exactly once, yet destructors - twice (this, of
> course, causes a crash in more complex setting).
>
> #include <stdio.h>
> #include <tuple>
>
> using namespace std;
>
> struct B
> {
> int d;
>
> B() { printf("c B\n"); }
> ~B() { printf("d B\n"); }
> void pp() { printf("pp BBB\n"); }
> };
>
> struct C
> {
> int e;
>
> C() { printf("c C\n"); }
> ~C() { printf("d C\n"); }
> };
>
> int main(int argc, char **argv)
> {
> //tuple<B, C> xx(B(), C());
> tuple<B, C> xx(make_tuple(B(), C()));
>
> printf("aaa\n");
> get<0>(xx).pp();
> }
Isn't it just that default copy constructors are called making copies of
objects so destructors are called twice: once for "original" object and
once for it's copy?
Regards
Tomasz Gajewski
More information about the Libstdc++
mailing list