non type template partial specialization

E

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.
 
I

Ian Collins

er said:
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(){};
 
E

er

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(){};

thanks.
 
E

er

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(){};

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<M> singleton;
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<0> singleton;
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
 
E

er

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(){};

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<M> singleton;
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<0> singleton;
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.
 
B

Bo Persson

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<M> singleton;
:: 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<0> singleton;
:: 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
 
E

er

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<M> singleton;
:: 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<0> singleton;
:: 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();
 
I

Ian Collins

er said:
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.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top