public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Problem with local struct
@ 2005-01-22  1:17 jlh
  2005-01-23 17:02 ` Gabriel Dos Reis
  0 siblings, 1 reply; 2+ messages in thread
From: jlh @ 2005-01-22  1:17 UTC (permalink / raw)
  To: gcc-help

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


I have troubles with a struct that is defined local to a function; see the code
below.  I compiled it as follows with gcc-3.4.3

$ g++ -c -Wall -pedantic -ansi -Wextra problem.cpp
problem.cpp: In function `void f()':
problem.cpp:22: error: no matching function for call to `bind2nd(f()::Func, int)'
********************************************************************************
However, if I move the entire "struct Func { ... }" block out of f() to global
scope, it compiles fine.  Therefore I assume there's nothing wrong with Func
itself, nor with the way it is being used in find_if().  I first thought that
maybe GCC doesn't fully support structs local to functions yet, but I guess
that's not the case, since I can properly instantiate and use Func, as shown
below.

Thanks,
jlh

/* ------------------------ snip ------------------------ */

#include <vector>
#include <functional>
#include <algorithm>

void f()
{
	struct Func : public std::binary_function<int, int, bool>
	{
		bool operator()(int, int) const
		{
			return true;
		}
	};

	/* works fine; can instantiate and use */
	Func f;
	f(0, 0);

	/* doesn't work: */
	std::vector<int> v;
	std::find_if(v.begin(), v.end(), std::bind2nd(Func(), 0));
}

/* ------------------------ snip ------------------------ */

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

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

* Re: Problem with local struct
  2005-01-22  1:17 Problem with local struct jlh
@ 2005-01-23 17:02 ` Gabriel Dos Reis
  0 siblings, 0 replies; 2+ messages in thread
From: Gabriel Dos Reis @ 2005-01-23 17:02 UTC (permalink / raw)
  To: jlh; +Cc: gcc-help

jlh <jlh@gmx.ch> writes:

| I have troubles with a struct that is defined local to a function; see the code
| below.  I compiled it as follows with gcc-3.4.3
| 
| $ g++ -c -Wall -pedantic -ansi -Wextra problem.cpp
| problem.cpp: In function `void f()':
| problem.cpp:22: error: no matching function for call to `bind2nd(f()::Func, int)'
| ********************************************************************************
| However, if I move the entire "struct Func { ... }" block out of f() to global
| scope, it compiles fine.  Therefore I assume there's nothing wrong with Func
| itself, nor with the way it is being used in find_if().  I first thought that
| maybe GCC doesn't fully support structs local to functions yet, but I guess
| that's not the case, since I can properly instantiate and use Func, as shown
| below.

The short answer is: values for type template parameters cannot be
local classes. 

The long answer is: while GCC rightly rejected the code, there was a
slight bug which prevented it  from giving a better diagnostic.  Fixed
on mainline.

-- Gaby

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

end of thread, other threads:[~2005-01-23 17:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-22  1:17 Problem with local struct jlh
2005-01-23 17:02 ` Gabriel Dos Reis

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