c++: too many templates?

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

c++: too many templates?

by @ :: Rate this Message:

| View Threaded | Show Only this Message

why the following code cannot be compiled?

template <typename TC>
class C
{
        template <typename TF>
        void f() {}
};

template <typename T>
void test()
{
        C<T> c;
        c.f<int>();
}

gcc-4.3.2 on "c.f<int>();" reports error: expected primary-expression before ‘int’

Re: c++: too many templates?

by Arturs Zoldners-2 :: Rate this Message:

| View Threaded | Show Only this Message

On Thu, 2008-10-23 at 04:14 -0700, @ wrote:

> why the following code cannot be compiled?
>
> template <typename TC>
> class C
> {
> template <typename TF>
> void f() {}
> };
>
> template <typename T>
> void test()
> {
> C<T> c;
> c.f<int>();
> }
>
> gcc-4.3.2 on "c.f<int>();" reports error: expected primary-expression before
> ‘int’

Try:

void test()
{
        C<T> c;
        c.template f<int>();
}

Regards,
Arturs Zoldners



Re: c++: too many templates?

by John S. Fine :: Rate this Message:

| View Threaded | Show Only this Message

In your real code, did you do something to make f public so test can use
it?  But that isn't the main problem.  Arturs Zoldners gave the solution
to the main problem:

If I understand correctly, while processing the definition of test, the
compiler doesn't look at the definition of C, so it doesn't know f is a
templated function, so it doesn't know how to parse the "<" in "c.f<int>".

When it instantiates test<T> it will, of course, look inside C<T> (for
at least the constructor of C and the declaration of f).  But if it
can't process test at definition time, it never gets to instantiation.

@ wrote:

> why the following code cannot be compiled?
>
> template <typename TC>
> class C
> {
> template <typename TF>
> void f() {}
> };
>
> template <typename T>
> void test()
> {
> C<T> c;
> c.f<int>();
> }
>
> gcc-4.3.2 on "c.f<int>();" reports error: expected primary-expression before
> ‘int’
>  


Re: c++: too many templates?

by @ :: Rate this Message:

| View Threaded | Show Only this Message

many thanks for your replies, it works

Re: c++: too many templates?

by Andrew Bell :: Rate this Message:

| View Threaded | Show Only this Message

2008/10/23 Arturs Zoldners <az@...>:

> On Thu, 2008-10-23 at 04:14 -0700, @ wrote:
>> why the following code cannot be compiled?
>>
>> template <typename TC>
>> class C
>> {
>>       template <typename TF>
>>       void f() {}
>> };
>>
>> template <typename T>
>> void test()
>> {
>>       C<T> c;
>>       c.f<int>();
>> }
>>
>> gcc-4.3.2 on "c.f<int>();" reports error: expected primary-expression before
>> 'int'
>
> Try:
>
> void test()
> {
>        C<T> c;
>        c.template f<int>();
> }

Is this syntax described in TC++PL?  I looked through my copy and
couldn't find it.

--
Andrew Bell
andrew.bell.ia@...