template and static field

C

CHAFIK Wassime

hi
how are u dealing with the heat????

i tried to compile the following code
/////////////////////////////////////////////////////////////////////////
template <typename V >
class MyTemplate{
public:
//typedef typename V::NestedType MyType;
static typename V::NestedType myStaticField;
};

class MyV{
public:
class NestedType {};
static void dummy(){};
};

typedef MyTemplate<MyV> MyTemplateInstance;

template<typename V>
MyV::NestedType MyTemplateInstance::myStaticField = MyV::NestedType();

int main(int argc, char* argv[]){
return 0;
}
////////////////////////////////////////////////////////////////////////

but i get the error:

testtemplate.cpp:17: error: template arguments to
‘MyTemplate<MyV>::myStaticField’ do not match original template
‘MyTemplate<V>::myStaticField


and i don't understand why...
thanx in advance for your help
 
A

Alf P. Steinbach /Usenet

* CHAFIK Wassime, on 26.08.2010 13:37:
hi
how are u dealing with the heat????

It's slightly rainy in Oslo.

i tried to compile the following code
/////////////////////////////////////////////////////////////////////////
template<typename V>
class MyTemplate{
public:
//typedef typename V::NestedType MyType;
static typename V::NestedType myStaticField;
};

class MyV{
public:
class NestedType {};
static void dummy(){};
};

typedef MyTemplate<MyV> MyTemplateInstance;

template<typename V>
MyV::NestedType MyTemplateInstance::myStaticField = MyV::NestedType();

int main(int argc, char* argv[]){
return 0;
}
////////////////////////////////////////////////////////////////////////

but i get the error:

testtemplate.cpp:17: error: template arguments to
‘MyTemplate<MyV>::myStaticField’ do not match original template
‘MyTemplate<V>::myStaticField


and i don't understand why...

Make that 'MyTemplate::myStaticField', not 'MyTemplateInstance::myStaticField',
plus change the initializer from 'MyV' to just 'V'.


Cheers & hth.,

- Alf
 
L

lucdanton

* CHAFIK Wassime, on 26.08.2010 13:37:
hi
how are u dealing with the heat????

I hear it's winter on the South hemisphere (well in the temperate
zones I guess).

[...]
Make that 'MyTemplate::myStaticField', not 'MyTemplateInstance::myStaticField',
plus change the initializer from 'MyV' to just 'V'.

Cheers & hth.,

- Alf

That should be a qualified 'typename V' right ? Both for the type and
the initializer.
 
A

Alf P. Steinbach /Usenet

* lucdanton, on 26.08.2010 17:32:
[...]
Make that 'MyTemplate::myStaticField', not 'MyTemplateInstance::myStaticField',
plus change the initializer from 'MyV' to just 'V'.

That should be a qualified 'typename V' right ? Both for the type and
the initializer.

Yah, plus also for the return type.

By the way, an easier way is to just define an inline accessor function, like

template< class V >
class MyTemplate
{
public:
static typename V::NestedType& someGoodName()
{
static typename V::NestedType theStatic;
return theStatic;
}
};


Cheers & hth.,

- Alf

PS: Please quote enough to establish context, and please don't quote signatures.
 
J

Johannes Schaub (litb)

CHAFIK said:
hi
how are u dealing with the heat????

i tried to compile the following code
/////////////////////////////////////////////////////////////////////////
template <typename V >
class MyTemplate{
public:
//typedef typename V::NestedType MyType;
static typename V::NestedType myStaticField;
};

class MyV{
public:
class NestedType {};
static void dummy(){};
};

typedef MyTemplate<MyV> MyTemplateInstance;

template<typename V>
MyV::NestedType MyTemplateInstance::myStaticField = MyV::NestedType();

int main(int argc, char* argv[]){
return 0;
}
////////////////////////////////////////////////////////////////////////

but i get the error:

testtemplate.cpp:17: error: template arguments to
‘MyTemplate<MyV>::myStaticField’ do not match original template
‘MyTemplate<V>::myStaticField

The type in the template has to match the one outside of it. The one within
is dependent, and the one in the out-of-line definition has to be that
dependent type too, that is, [clause-#1-param-#1]::NestedType . So, you
need

template <typename V >
class MyTemplate{
public:
//typedef typename V::NestedType MyType;
static typename V::NestedType myStaticField;
};

template<typename V>
typename V::NestedType MyTemplate<V>::myStaticField =
typename V::NestedType();

class MyV{
public:
class NestedType {};
static void dummy(){};
};

typedef MyTemplate<MyV> MyTemplateInstance;
// no need to define anything here!
 
C

CHAFIK Wassime

thanks for all
i thought that the initialisation is left for the template instance
thanks for your help
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top