GCC cannot support local classes together with template.

View: New views
3 Messages — Rating Filter:   Alert me  

GCC cannot support local classes together with template.

by Jim King :: Rate this Message:

| View Threaded | Show Only this Message

Hi everyone:

The following code cannot be compiled by GCC 4.3.3.



#include <iostream>
#include <algorithm>
#include <functional>


using namespace std;


int main()
{
    class PrintOne : unary_function<int, void>
    {
    public:
        result_type operator()(argument_type i)
        {
            wcout << i << L'\n';
        }
    };
    int a[20] = {0, 0, 0, 0, 0};
    for_each(a, a + 20, PrintOne());

    return 0;
}



The error message is:
test.cpp:22: error: no matching function for call to `for_each(int[20], int*, main()::PrintOne)'


If we move class PrintOne outside or do not use template function, the compilation will succeed.

Is the behavior a bug or as design? Thanks.

Re: GCC cannot support local classes together with template.

by Stephen M. Webb :: Rate this Message:

| View Threaded | Show Only this Message

On 13/03/09 00:05, Jim King wrote:
> If we move class PrintOne outside or do not use template function, the
> compilation will succeed.
>
> Is the behavior a bug or as design? Thanks.

[14.3.1.](2) explicitly says using a local name as a template argument is not
allowed.

Follow-up to gcc-help@....

--
Stephen M. Webb
stephen.webb@...

Re: GCC cannot support local classes together with template.

by Jim King :: Rate this Message:

| View Threaded | Show Only this Message

Thank you very much.


Stephen M. Webb wrote:
On 13/03/09 00:05, Jim King wrote:
> If we move class PrintOne outside or do not use template function, the
> compilation will succeed.
>
> Is the behavior a bug or as design? Thanks.

[14.3.1.](2) explicitly says using a local name as a template argument is not
allowed.

Follow-up to gcc-help@gcc.gnu.org.

--
Stephen M. Webb
stephen.webb@bregmasoft.ca