This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Weird (unintuitive?) behavior of std::tuple


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]