473,320 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2947
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
4
by: TT \(Tom Tempelaere\) | last post by:
Comeau compiler complains (too few arguments for class template "B") at line *** #include <memory> template<typename T, size_t n> struct A {}; template<typename T, size_t n> struct B;
3
by: Steve Brown | last post by:
Hello all, Is there a way to determine a variable's type at run-time? The reason I'm asking is that i have code that looks like this: template <class T> Object::Object(int TypeCode, T* data)...
1
by: BekTek | last post by:
I'm still confused about the template partial specialization which is used in many libraries.. due to lack of introduction for beginner.. Could you tell me about that in short? Thanks in...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
2
by: Michael Stembera | last post by:
Here is a very simple piece of code to repro this bug. template<typename T, int N> inline bool foo( void ) { return true; } template<typename T> inline bool foo<T, 1>( void ) { return...
4
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
6
by: Andre Kempe | last post by:
hej folks. i have a heap with fixed size and want to determine the depth of a element with given index at compile-time. therefore i wrote some templates. however, when i use template...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.