problem with structure in function

Z

Zoran Stipanicev

Hi!
I have a code that looks some thing like this

void function()
{
struct st1
{
template<...>
struct st2
{
static void func2(){...}
}
}
}

I'm using VC++ 2003 and I get the error some thing like:
You can't have template in structure defined in function

Does anyone know if this defined in standard or VC++ 2003 is not fully
supporting standard in this case.

Thx!

P.S. I need that so that objects defined in function would be in global
scope of structure, and structure is actually body of static for loop
 
S

Sharad Kala

Zoran Stipanicev said:

Merry Christmas to all readers of comp.lang.c++.
I have a code that looks some thing like this

void function()
{
struct st1
{
template<...>
struct st2
{
static void func2(){...}
}
}
}

I'm using VC++ 2003 and I get the error some thing like:
You can't have template in structure defined in function

Does anyone know if this defined in standard or VC++ 2003 is not fully
supporting standard in this case.

VC++ is correct. 14.5.2/2 reads - " local class shall not have member
templates."

Sharad
 
S

Sagar Choudhary

Sharad said:
Merry Christmas to all readers of comp.lang.c++.

I tried the same code with Microsofts Visual studio 6
it works perfectly fine
I even added a template class and a template function within the
structure but didn't get any errors. I don't understand how at one can
compile to standards at one point and not at other.
VC++ is correct. 14.5.2/2 reads - " local class shall not have member
templates."

Sharad
14.5.2/1 states that a template class can be declared within a class or
template
I modified the function something like this and it compiled sucessfully
void function()
{
struct st1
{
template<class T> sort(T);
struct st2
{
template<class T, int i> class Buffer {
T v ;
int sz;
public:
Buffer() : sz(i) {}
// ...
};
static void func2(){}
};
};

}

Can you please explain how ?
 
S

Sharad Kala

Sagar Choudhary said:
I tried the same code with Microsofts Visual studio 6
it works perfectly fine


Who told you that MS VC 6 is compliant to the Standard in this regard ? In
fact it is quite infamous for its template support. Hang on here for some
time and you will know it.
I even added a template class and a template function within the
structure but didn't get any errors. I don't understand how at one can
compile to standards at one point and not at other.

Compilers have a good property like (most) human beings and that's to learn
from their mistakes. VC++ 7.0 got a little close to the Standard, 7.1 still
closer and 8 is still more closer.

VC++ is correct. 14.5.2/2 reads - " local class shall not have member
templates."

Sharad
14.5.2/1 states that a template class can be declared within a class or
template
I modified the function something like this and it compiled sucessfully
void function()
{
struct st1
{
template<class T> sort(T);
struct st2
{
template<class T, int i> class Buffer {
T v ;
int sz;
public:
Buffer() : sz(i) {}
// ...
};
static void func2(){}
};
};

}

Can you please explain how ?


All I can say is that don't believe your compilers all the time.

Sharad
 
I

Ioannis Vranos

Sagar Choudhary wrote:

Your code fixed does not compile with GCC 3.3.1 too:



void function()
{
struct st1
{
template<class T>
struct st2
{
static void func2(){}
};
};
}



int main()
{
function();
}


C:\c\temp.cpp In function `void function()':
9 C:\c\temp.cpp invalid declaration of member template `struct
function()::st1::st2<T>' in local class
 

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

Forum statistics

Threads
474,189
Messages
2,571,016
Members
47,616
Latest member
gijoji4272

Latest Threads

Top