This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Weird (unintuitive?) behavior of std::tuple
- From: Tomasz Gajewski <tomga at wp dot pl>
- To: Alex Dubov <oakad at yahoo dot com>
- Cc: libstdc++ at gcc dot gnu dot org
- Date: Sat, 30 Oct 2010 15:43:09 +0200
- Subject: Re: Weird (unintuitive?) behavior of std::tuple
- References: <loom.20101029T194502-571@post.gmane.org>
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