public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* unique_ptr w/ custom deleter in header
@ 2013-01-19  9:48 Nick
  2013-01-19 13:26 ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Nick @ 2013-01-19  9:48 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1857 bytes --]

When I use a std::unique_ptr w/ a custom deleter via an inline lambda in
a non-template function implemented inline in a header file, I get a
linker error.

I'm using gcc 4.7.2 on Linux.

Here (and attached) is a sample which shows 3 variations of the usage.  

Note that the specifics of this example are only for demonstration
purposes--I'm not actually trying to use a custom deleter to perform
what the default implementation does.

//----------------------------------------------------------

nick@nimble ~/UniquePtrTest $ cat ./main.cpp 
#include <memory>

#include "UniquePtrTest.h"

void Foo1()
{
	std::unique_ptr<char, void(*)(char*)> p(
		new char[5],
		[](char * p)
		{
			delete(p);
		}
	);
}

int main()
{
	Foo1();
	Foo2<int>();
	Foo3();
	Foo4();
}
//----------------------------------------------------------

nick@nimble ~/UniquePtrTest $ cat ./UniquePtrTest.h 
#include <memory>		// for std::unique_ptr


template<typename T>
void Foo2()
{
	std::unique_ptr<char, void(*)(char*)> p(
		new char[5],
		[](char * p)
		{
			delete(p);
		}
	);
}

inline
void Foo3()
{
	auto myDeleter = [](char * p)
	{
		delete p;
	};
	std::unique_ptr<char, decltype(myDeleter)> p(
		new char[5],
		myDeleter
	);
}

inline
void Foo4()
{
	std::unique_ptr<char, void(*)(char*)> p(
		new char[5],
		[](char * p)
		{
			delete(p);
		}
	);
}
//----------------------------------------------------------


Foo1, Foo2, and Foo3 all compile and link fine.  Only Foo4 yields a
linker error:

nick@nimble ~/UniquePtrTest $ g++ -std=c++11  ./main.cpp 
/tmp/cc98v9Mr.o: In function `Foo4()::{lambda(char*)#1}::operator void (*)(char*)() const':
main.cpp:(.text._ZZ4Foo4vENKUlPcE_cvPFvS_EEv[_ZZ4Foo4vENKUlPcE_cvPFvS_EEv]+0x4): undefined reference to `Foo4()::{lambda(char*)#1}::_FUN(char*)'
collect2: error: ld returned 1 exit status


Is this expected?


Best regards,
Nick


[-- Attachment #2: main.cpp --]
[-- Type: text/x-c++src, Size: 218 bytes --]

#include <memory>

#include "UniquePtrTest.h"

void Foo1()
{
	std::unique_ptr<char, void(*)(char*)> p(
		new char[5],
		[](char * p)
		{
			delete(p);
		}
	);
}

int main()
{
	Foo1();
	Foo2<int>();
	Foo3();
	Foo4();
}

[-- Attachment #3: UniquePtrTest.h --]
[-- Type: text/x-chdr, Size: 453 bytes --]

#include <memory>		// for std::unique_ptr


template<typename T>
void Foo2()
{
	std::unique_ptr<char, void(*)(char*)> p(
		new char[5],
		[](char * p)
		{
			delete(p);
		}
	);
}

inline
void Foo3()
{
	auto myDeleter = [](char * p)
	{
		delete p;
	};
	std::unique_ptr<char, decltype(myDeleter)> p(
		new char[5],
		myDeleter
	);
}

inline
void Foo4()
{
	std::unique_ptr<char, void(*)(char*)> p(
		new char[5],
		[](char * p)
		{
			delete(p);
		}
	);
}

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: unique_ptr w/ custom deleter in header
  2013-01-19  9:48 unique_ptr w/ custom deleter in header Nick
@ 2013-01-19 13:26 ` Jonathan Wakely
  2013-01-19 16:41   ` Nick
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2013-01-19 13:26 UTC (permalink / raw)
  To: Nick; +Cc: gcc-help

On Jan 19, 2013 3:22 AM, "Nick"  wrote:
>
> When I use a std::unique_ptr w/ a custom deleter via an inline lambda in
> a non-template function implemented inline in a header file, I get a
> linker error.

The error you're getting is a known bug with lambdas, you can find
multiple issues like it in bugzilla.

> Note that the specifics of this example are only for demonstration
> purposes--I'm not actually trying to use a custom deleter to perform
> what the default implementation does.

It's good that it's only for demonstration because your deleters have
undefined behaviour, they mix the array form of 'new' with the
non-array 'delete'.

Usually the unique_ptr<T[]> specialization should be used for arrays.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: unique_ptr w/ custom deleter in header
  2013-01-19 13:26 ` Jonathan Wakely
@ 2013-01-19 16:41   ` Nick
  2013-01-19 18:26     ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Nick @ 2013-01-19 16:41 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-help

On Sat, 2013-01-19 at 13:24 +0000, Jonathan Wakely wrote:
> On Jan 19, 2013 3:22 AM, "Nick"  wrote:
> >
> > When I use a std::unique_ptr w/ a custom deleter via an inline lambda in
> > a non-template function implemented inline in a header file, I get a
> > linker error.
> 
> The error you're getting is a known bug with lambdas, you can find
> multiple issues like it in bugzilla.

Thanks for the info Jonathan.  I'm a bit surprised I didn't come across
these issue via Google searches.  I'll look directly in bugzilla.

So for clarity, are you saying that this is a known bug w/ gcc's support
for lambdas, or something larger (ie. w/ the spec)?

> 
> > Note that the specifics of this example are only for demonstration
> > purposes--I'm not actually trying to use a custom deleter to perform
> > what the default implementation does.
> 
> It's good that it's only for demonstration because your deleters have
> undefined behaviour, they mix the array form of 'new' with the
> non-array 'delete'.
> 
> Usually the unique_ptr<T[]> specialization should be used for arrays.

Right, thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: unique_ptr w/ custom deleter in header
  2013-01-19 16:41   ` Nick
@ 2013-01-19 18:26     ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2013-01-19 18:26 UTC (permalink / raw)
  To: Nick; +Cc: gcc-help

On 19 January 2013 16:17, Nick wrote:
> So for clarity, are you saying that this is a known bug w/ gcc's support
> for lambdas, or something larger (ie. w/ the spec)?

It's just a G++ bug, possibly this one:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55710

Or one of the others linked to from the meta-bug for tracking lambda
bugs: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54367

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-01-19 16:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-19  9:48 unique_ptr w/ custom deleter in header Nick
2013-01-19 13:26 ` Jonathan Wakely
2013-01-19 16:41   ` Nick
2013-01-19 18:26     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).