Typeof and pointer to templated functions/method

May 14, 2008 at 12:44pm
Hi all,
someone knows why the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct A
{
    template <typename T>
    void foo()
    {
    }
};

template <typename T>
void bar()
{
}

int main()
{
    typedef typeof(&A::foo<int>) foo_t;
    typedef typeof(&bar<int>)    bar_t;
}


produces these errors? (compiled with gcc 4.2.3)

to_check.cpp: In function ‘int main()’:
to_check.cpp:16: error: type of ‘& foo<int>’ is unknown
to_check.cpp:16: error: invalid type in declaration before ‘;’ token
to_check.cpp:17: error: type of ‘& bar<int>’ is unknown
to_check.cpp:17: error: invalid type in declaration before ‘;’ token

The compiler should be able to determine the type of an instance of a pointer to function/method template. Right?

Thanks in advance!
Bye, Tarch
May 22, 2008 at 7:08am
Bump. :D

Bye!
May 22, 2008 at 11:56am
typeof is not, to the best of my knowledge generally supported in C++. I think it's going to be in the next version, or maybe it's just been added in the most recent. Maybe someone can confirm one way or the other. That said I've used an ancient version of g++ (2.95.3) and it will happily compile it.

You say you compiled it with gcc 4.2.3, is that gcc or g++ you used? If it was gcc it should be g++. gcc is the C compiler, g++ is the C++ compiler
Last edited on May 22, 2008 at 11:59am
May 22, 2008 at 2:06pm

typeof is not, to the best of my knowledge generally supported in C++.

It could be maybe...


You say you compiled it with gcc 4.2.3, is that gcc or g++ you used? If it was gcc it should be g++. gcc is the C compiler, g++ is the C++ compiler


I'd used g++ with gcc I meant the whole compiler suite, I've tried also today (version 3.3.6 and 4.1.2) and I get the same errors.

Thanks anyway.
Bye, Tarch!
May 22, 2008 at 2:34pm
The typeof operator, AFAIK, was a GCC extension.
It no longer works on templated objects as of GCC 3.2.3.
Bug report: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9459
Release changes: http://www.gnu.org/software/gcc/gcc-3.2/changes.html

Currently GCC seems to have a really hard time determining the type of template functions even in legitimate circumstances...

Fooey.
May 23, 2008 at 12:16pm
Try removing struct declaration and recompile it.
Meaning, do not declare/define the template within a struct scope.

A template is a generic functionality defined across data types. I would not use a struct to define such generic functionality within a limited scope.

Check it out. Good luck :)
May 23, 2008 at 2:32pm
Some times we need to have a specialized templates within a specific scope, in which case, it would be used within that scope.
But you are trying to use it from main, which is out of struct's scope.

If you want to have such generic template within the scope, you better try to use it within the struct's scope and redesign the code.

Check it out. Good luck :)
Topic archived. No new replies allowed.