468,310 Members | 1,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,310 developers. It's quick & easy.

non type template partial specialization

er
hi,

could someone please help with this code?

template<unsigned int N,unsigned int M>
class A{
public:
A();

};

template<unsigned int N,unsigned int M>
A<N,M>::A(){};//fine

//intended: partial specialization
template<unsigned int N>
A<N,0>::A(){};//not fine//see compiler error below

make -k all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -
MT"main.d" -o"main.o" "../main.cpp"
.../main.cpp:6:3: warning: no newline at end of file
.../header.hpp:16: error: invalid use of undefined type 'class A<N,
0u>'
.../header.hpp:5: error: declaration of 'class A<N, 0u>'
.../header.hpp:16: error: template definition of non-template 'A<N,
0u>::A()'
make: *** [main.o] Error 1
make: Target `all' not remade because of errors.

Nov 8 '07 #1
7 2435
er wrote:
hi,

could someone please help with this code?

template<unsigned int N,unsigned int M>
class A{
public:
A();

};

template<unsigned int N,unsigned int M>
A<N,M>::A(){};//fine

//intended: partial specialization
template<unsigned int N>
A<N,0>::A(){};//not fine//see compiler error below
You can't specialise a member of a class template, you have to
specialise the class template:

template<unsigned int N>
class A<N,0>
{
public:
A();
};

template<unsigned int N>
A<N,0>::A(){};

--
Ian Collins.
Nov 9 '07 #2
er
On Nov 8, 11:34 pm, Ian Collins <ian-n...@hotmail.comwrote:
er wrote:
hi,
could someone please help with this code?
template<unsigned int N,unsigned int M>
class A{
public:
A();
};
template<unsigned int N,unsigned int M>
A<N,M>::A(){};//fine
//intended: partial specialization
template<unsigned int N>
A<N,0>::A(){};//not fine//see compiler error below

You can't specialise a member of a class template, you have to
specialise the class template:

template<unsigned int N>
class A<N,0>
{
public:
A();

};

template<unsigned int N>
A<N,0>::A(){};

--
Ian Collins.
thanks.

Nov 9 '07 #3
er
On Nov 8, 11:34 pm, Ian Collins <ian-n...@hotmail.comwrote:
er wrote:
hi,
could someone please help with this code?
template<unsigned int N,unsigned int M>
class A{
public:
A();
};
template<unsigned int N,unsigned int M>
A<N,M>::A(){};//fine
//intended: partial specialization
template<unsigned int N>
A<N,0>::A(){};//not fine//see compiler error below

You can't specialise a member of a class template, you have to
specialise the class template:

template<unsigned int N>
class A<N,0>
{
public:
A();

};

template<unsigned int N>
A<N,0>::A(){};

--
Ian Collins.
following up with this: what i'd like to implement is a recursion, but
as below it does not work...

template<unsigned int M>
class A{
public:
static A<M>& instance();
private:
A();
};
template<unsigned int M>
A<M>& A<M>::instance(){
static A<Msingleton;
return singleton;
};
template<unsigned int M>
A<M>::A(){};//WITHOUT RECURSION. FINE
//A<M>::A(){A<M-1>& ref = A<M-1>::instance();};//RECURSION. NOT FINE.
template<>
class A<0>{
public:
static A<0>& instance(){
static A<0singleton;
return singleton;
};
private:
A(){};
};

.../header.hpp: In constructor 'A<M>::A() [with unsigned int M = 1u]':
.../header.hpp:15: instantiated from 'static A<M>& A<M>::instance()
[with unsigned int M = 1u]'
.../main.cpp:5: instantiated from here

Nov 10 '07 #4
er
On Nov 9, 6:55 pm, er <erwann.rog...@gmail.comwrote:
On Nov 8, 11:34 pm, Ian Collins <ian-n...@hotmail.comwrote:
er wrote:
hi,
could someone please help with this code?
template<unsigned int N,unsigned int M>
class A{
public:
A();
};
template<unsigned int N,unsigned int M>
A<N,M>::A(){};//fine
//intended: partial specialization
template<unsigned int N>
A<N,0>::A(){};//not fine//see compiler error below
You can't specialise a member of a class template, you have to
specialise the class template:
template<unsigned int N>
class A<N,0>
{
public:
A();
};
template<unsigned int N>
A<N,0>::A(){};
--
Ian Collins.

following up with this: what i'd like to implement is a recursion, but
as below it does not work...

template<unsigned int M>
class A{
public:
static A<M>& instance();
private:
A();};

template<unsigned int M>
A<M>& A<M>::instance(){
static A<Msingleton;
return singleton;};

template<unsigned int M>
A<M>::A(){};//WITHOUT RECURSION. FINE
//A<M>::A(){A<M-1>& ref = A<M-1>::instance();};//RECURSION. NOT FINE.
template<>
class A<0>{
public:
static A<0>& instance(){
static A<0singleton;
return singleton;
};
private:
A(){};

};

../header.hpp: In constructor 'A<M>::A() [with unsigned int M = 1u]':
../header.hpp:15: instantiated from 'static A<M>& A<M>::instance()
[with unsigned int M = 1u]'
../main.cpp:5: instantiated from here
from what i see online enum is preferable to static for recursive
template. still, any help welcomed.

Nov 10 '07 #5
er wrote:
::
:: following up with this: what i'd like to implement is a recursion,
:: but as below it does not work...
::
:: template<unsigned int M>
:: class A{
:: public:
:: static A<M>& instance();
:: private:
:: A();
:: };
:: template<unsigned int M>
:: A<M>& A<M>::instance(){
:: static A<Msingleton;
:: return singleton;
:: };
:: template<unsigned int M>
:: A<M>::A(){};//WITHOUT RECURSION. FINE
:: //A<M>::A(){A<M-1>& ref = A<M-1>::instance();};//RECURSION. NOT
:: FINE. template<>
:: class A<0>{
:: public:
:: static A<0>& instance(){
:: static A<0singleton;
:: return singleton;
:: };
:: private:
:: A(){};
:: };
::
:: ../header.hpp: In constructor 'A<M>::A() [with unsigned int M =
:: 1u]': ../header.hpp:15: instantiated from 'static A<M>&
:: A<M>::instance() [with unsigned int M = 1u]'
:: ../main.cpp:5: instantiated from here

You don't show what's in the main() function. Perhaps the problem lies
there?

The code above looks pretty ok to me.
Bo Persson
Nov 10 '07 #6
er
On Nov 10, 7:22 am, "Bo Persson" <b...@gmb.dkwrote:
er wrote:

::
:: following up with this: what i'd like to implement is a recursion,
:: but as below it does not work...
::
:: template<unsigned int M>
:: class A{
:: public:
:: static A<M>& instance();
:: private:
:: A();
:: };
:: template<unsigned int M>
:: A<M>& A<M>::instance(){
:: static A<Msingleton;
:: return singleton;
:: };
:: template<unsigned int M>
:: A<M>::A(){};//WITHOUT RECURSION. FINE
:: //A<M>::A(){A<M-1>& ref = A<M-1>::instance();};//RECURSION. NOT
:: FINE. template<>
:: class A<0>{
:: public:
:: static A<0>& instance(){
:: static A<0singleton;
:: return singleton;
:: };
:: private:
:: A(){};
:: };
::
:: ../header.hpp: In constructor 'A<M>::A() [with unsigned int M =
:: 1u]': ../header.hpp:15: instantiated from 'static A<M>&
:: A<M>::instance() [with unsigned int M = 1u]'
:: ../main.cpp:5: instantiated from here

You don't show what's in the main() function. Perhaps the problem lies
there?

The code above looks pretty ok to me.

Bo Persson
thanks.

what i have in main() is an instance such as:

A<1>& a1 = A<1>::instance();

Nov 10 '07 #7
er wrote:
>
what i have in main() is an instance such as:

A<1>& a1 = A<1>::instance();
I can't see anything wrong with your code and adding

int main(){
A<1>& a1 = A<1>::instance();
}

Doesn't give me any diagnostics (with your not fine line uncommented)
with Sun CC or gcc.

Post a complete example and error messages.

--
Ian Collins.
Nov 11 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Agent Mulder | last post: by
4 posts views Thread by TT \(Tom Tempelaere\) | last post: by
3 posts views Thread by Steve Brown | last post: by
6 posts views Thread by Andre Kempe | last post: by
9 posts views Thread by stephen.diverdi | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.