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]

Weird (unintuitive?) behavior of std::tuple


Greetings.

It well may be that I'm missing something, but trying to use <tuple> in my code
results in really weird stuff. I presume, somebody, somewhere explained this
issue, but I just couldn't find the answer, so I'll ask it here.

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).

Attempt to use different syntax, "tuple<B, C> xx(B(), C());", will cause the
compilation to fail, being unable to resolve the get<> function template.

However, if B and C had non-default constructors, this patter "tuple<B, C>
xx(B(1), C(2));" will allow the program to compile (get<> will be resolved
correctly), yet it will exhibit the already mentioned run-time problem
(constructors invoked once, destructors - twice).

Similar bad things happen when I try to use tuple<> as a class member.

The example is attached hereby:

#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();
}



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