Reprise: Template syntax help

D

Dave

Hello,

Below, I have included a short program that does not compile. The
problematic line is indicated with a comment. Comeau produces this error:

"ComeauTest.c", line 19: error: type name is not allowed
T::bar_t<int> local;
^
Please note that the answer is *not* to preface the offending line with
"typename".

This is a problem with lookup of dependent names, and I think the
".template" construct is needed somehow. But I can't seem to get the right
syntax...

Thanks!
Dave


#include <iostream>

using namespace std;

struct foo_t
{
template <typename T>
struct bar_t
{
T m_var;
};
};

template <typename T>
struct geeker
{
void func()
{
T::bar_t<int> local; // Error here!

local.m_var = 42;
cout << local.m_var << endl;
}
};

int main()
{
geeker<foo_t> r;

r.func();
}
 
V

Victor Bazarov

Dave said:
Below, I have included a short program that does not compile. The
problematic line is indicated with a comment. Comeau produces this error:

"ComeauTest.c", line 19: error: type name is not allowed
T::bar_t<int> local;
^
Please note that the answer is *not* to preface the offending line with
"typename".

This is a problem with lookup of dependent names, and I think the
".template" construct is needed somehow. But I can't seem to get the right
syntax...

Thanks!
Dave


#include <iostream>

using namespace std;

struct foo_t
{
template <typename T>
struct bar_t
{
T m_var;
};
};

template <typename T>
struct geeker
{
void func()
{
T::bar_t<int> local; // Error here!

VC++ accepts it without 'template', but Comeau shuts up if I say

typename T:: template bar_t said:
local.m_var = 42;
cout << local.m_var << endl;
}
};

int main()
{
geeker<foo_t> r;

r.func();
}

"If at first you don't succeed, try and try again..."

V
 
A

Ali Cehreli

Please note that the answer is *not* to preface the offending line with
"typename".
struct foo_t
{
template <typename T>
struct bar_t
{
T m_var;
};
};

template <typename T>
struct geeker
{
void func()
{
T::bar_t<int> local; // Error here!

This works:

typename T:: template bar_t<int> local;

Ali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top