hi,
could someone please help with this code?
template<unsign ed int N,unsigned int M>
class A{
public:
A();
};
template<unsign ed int N,unsigned int M>
A<N,M>::A(){};//fine
//intended: partial specialization
template<unsign ed 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. 7 2985
er wrote:
hi,
could someone please help with this code?
template<unsign ed int N,unsigned int M>
class A{
public:
A();
};
template<unsign ed int N,unsigned int M>
A<N,M>::A(){};//fine
//intended: partial specialization
template<unsign ed 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<unsign ed int N>
class A<N,0>
{
public:
A();
};
template<unsign ed int N>
A<N,0>::A(){};
--
Ian Collins.
On Nov 8, 11:34 pm, Ian Collins <ian-n...@hotmail.co mwrote:
er wrote:
hi,
could someone please help with this code?
template<unsign ed int N,unsigned int M>
class A{
public:
A();
};
template<unsign ed int N,unsigned int M>
A<N,M>::A(){};//fine
//intended: partial specialization
template<unsign ed 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<unsign ed int N>
class A<N,0>
{
public:
A();
};
template<unsign ed int N>
A<N,0>::A(){};
--
Ian Collins.
thanks.
On Nov 8, 11:34 pm, Ian Collins <ian-n...@hotmail.co mwrote:
er wrote:
hi,
could someone please help with this code?
template<unsign ed int N,unsigned int M>
class A{
public:
A();
};
template<unsign ed int N,unsigned int M>
A<N,M>::A(){};//fine
//intended: partial specialization
template<unsign ed 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<unsign ed int N>
class A<N,0>
{
public:
A();
};
template<unsign ed 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<unsign ed int M>
class A{
public:
static A<M>& instance();
private:
A();
};
template<unsign ed int M>
A<M>& A<M>::instance( ){
static A<Msingleton;
return singleton;
};
template<unsign ed 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
On Nov 9, 6:55 pm, er <erwann.rog...@ gmail.comwrote:
On Nov 8, 11:34 pm, Ian Collins <ian-n...@hotmail.co mwrote:
er wrote:
hi,
could someone please help with this code?
template<unsign ed int N,unsigned int M>
class A{
public:
A();
};
template<unsign ed int N,unsigned int M>
A<N,M>::A(){};//fine
//intended: partial specialization
template<unsign ed 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<unsign ed int N>
class A<N,0>
{
public:
A();
};
template<unsign ed 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<unsign ed int M>
class A{
public:
static A<M>& instance();
private:
A();};
template<unsign ed int M>
A<M>& A<M>::instance( ){
static A<Msingleton;
return singleton;};
template<unsign ed 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.
er wrote:
::
:: following up with this: what i'd like to implement is a recursion,
:: but as below it does not work...
::
:: template<unsign ed int M>
:: class A{
:: public:
:: static A<M>& instance();
:: private:
:: A();
:: };
:: template<unsign ed int M>
:: A<M>& A<M>::instance( ){
:: static A<Msingleton;
:: return singleton;
:: };
:: template<unsign ed 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
On Nov 10, 7:22 am, "Bo Persson" <b...@gmb.dkwro te:
er wrote:
::
:: following up with this: what i'd like to implement is a recursion,
:: but as below it does not work...
::
:: template<unsign ed int M>
:: class A{
:: public:
:: static A<M>& instance();
:: private:
:: A();
:: };
:: template<unsign ed int M>
:: A<M>& A<M>::instance( ){
:: static A<Msingleton;
:: return singleton;
:: };
:: template<unsign ed 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( );
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 specialize
Music<Bach>, I expect that the original play() method is available
in the specialization, but it is not. How can I fix this?
-X
|
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;
|
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)
{
switch (TypeCode)
case 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 advance..
|
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>
| |
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 false;
|
by: Alfonso Morra |
last post by:
Does VC 7.1 support template specialization and partial specialization ?
|
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 assign() function really a
specialization of the first assign() or is it an assign() overload?
Thank you.
-- Marek
|
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 index_properties<unsigned int>,
i get a compiler-error, complaining about the template-recursion of
__index_properties__. when i add a partially specialized template (the
three commented lines) to stop the template-recursion, it works.
does anyone how to...
|
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.
//------------------------------------------------------------------
// my regular glass
class A { };
// my templated class
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |