I have the following program using templates. Someone please let me know
the syntax to be used for defining the member functions push, pop etc.
as non-inline functions.
#include <iostream>
using namespace std;
template<class T, int size = 50>
class Stack
{
private:
//enum {ssize = 20};
T stack[size];
int top;
public:
Stack () : top(0) {}
T& operator[] (int index)
{
return stack[index];
}
void push (T n)
{
stack[top++] = n;
}
T pop ()
{
return stack[--top];
}
};
int main()
{
Stack<int> intStack;
cout<<"Size of intStack = "<<sizeof intStack<<endl;
for (int i=0; i<20; i++)
{
intStack.push (i*2);
}
for (int i=0; i<20; i++)
{
cout<<"i = "<<intStack.pop()<<endl;
}
return 0;
}
TIA,
Senthilraja. 7 2656
On Mon, 13 Oct 2003 10:06:58 +0530, Senthilraja
<se**********@adcc.alcatel.be> wrote: I have the following program using templates. Someone please let me know the syntax to be used for defining the member functions push, pop etc. as non-inline functions.
#include <iostream> using namespace std;
template<class T, int size = 50> class Stack { private: //enum {ssize = 20}; T stack[size]; int top; public: Stack () : top(0) {} T& operator[] (int index) { return stack[index]; } void push (T n) { stack[top++] = n; }
T pop () { return stack[--top]; }
};
I'm sure someone can quote the relevant part of the Standard, but for
your purposes, here's a practical example.
// Definition
template <class T, int size>
void Stack<T, size>::push(T n)
{
// Implementation goes here
}
Remember, the full template definition must be present for the
programs to use, so you need to include the definitions, even
non-inline, in the header file or then take advantage of the poorly
supported export-keyword.
You might also want to A) get a book on C++ or B) get a new one if you
already have one and it does not include discussion on this.
e wrote: On Mon, 13 Oct 2003 10:06:58 +0530, Senthilraja <se**********@adcc.alcatel.be> wrote:
I have the following program using templates. Someone please let me know the syntax to be used for defining the member functions push, pop etc. as non-inline functions.
#include <iostream> using namespace std;
template<class T, int size = 50> class Stack { private: //enum {ssize = 20}; T stack[size]; int top; public: Stack () : top(0) {} T& operator[] (int index) { return stack[index]; } void push (T n) { stack[top++] = n; }
T pop () { return stack[--top]; }
};
I'm sure someone can quote the relevant part of the Standard, but for your purposes, here's a practical example.
// Definition template <class T, int size> void Stack<T, size>::push(T n) { // Implementation goes here }
Remember, the full template definition must be present for the programs to use, so you need to include the definitions, even non-inline, in the header file or then take advantage of the poorly supported export-keyword.
You might also want to A) get a book on C++ or B) get a new one if you already have one and it does not include discussion on this.
Thanks very much.
> >I have the following program using templates. Someone please let me
know the syntax to be used for defining the member functions push, pop
etc.as non-inline functions.
<snip> I'm sure someone can quote the relevant part of the Standard, but for your purposes, here's a practical example.
// Definition template <class T, int size> void Stack<T, size>::push(T n) { // Implementation goes here }
Remember, the full template definition must be present for the programs to use, so you need to include the definitions, even non-inline, in the header file or then take advantage of the poorly supported export-keyword.
That would violate the One Definition Rule (discussed in paragraph 3.2
of the standard). If you include the definition in the header file you
must use the inline keyword to get around the ODR. If you don't want to
do define template functions inline, you need a compiler that supports
the export keyword, such as the Comeau compiler
( http://www.comeaucomputing.com).
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
"Peter van Merkerk" <me*****@deadspam.com> wrote in message news: Remember, the full template definition must be present for the programs to use, so you need to include the definitions, even non-inline, in the header file or then take advantage of the poorly supported export-keyword.
That would violate the One Definition Rule (discussed in paragraph 3.2 of the standard). If you include the definition in the header file you must use the inline keyword to get around the ODR. If you don't want to do define template functions inline, you need a compiler that supports the export keyword, such as the Comeau compiler (http://www.comeaucomputing.com).
Hm. Looking at the Standard Libraries provided by GCC and STLport, and
'Modern C++ Design' (the only C++ book I own so I don't have any more
sources, sorry) seem to place the non-inline definitions in the header
files.
None of my programs have suffered from me doing this, but I don't know
if that's just the compiler or if the standard maybe has some
exclusions for templates. I did read about Comeau's export support,
but to me the solution seemed a bit hackish? How is it in production
use?
> > > Remember, the full template definition must be present for the programs to use, so you need to include the definitions, even non-inline, in the header file or then take advantage of the
poorly supported export-keyword. That would violate the One Definition Rule (discussed in paragraph
3.2 of the standard). If you include the definition in the header file
you must use the inline keyword to get around the ODR. If you don't want
to do define template functions inline, you need a compiler that
supports the export keyword, such as the Comeau compiler (http://www.comeaucomputing.com).
Hm. Looking at the Standard Libraries provided by GCC and STLport, and 'Modern C++ Design' (the only C++ book I own so I don't have any more sources, sorry) seem to place the non-inline definitions in the header files.
That's funny, the copy I have of STLport and the Loki library (from
'Modern C++ Design') uses inline implementations for templated (member)
functions.
None of my programs have suffered from me doing this, but I don't know if that's just the compiler or if the standard maybe has some exclusions for templates.
I'm not a language lawyer, but I haven't seen anything in the chapter
about the One Definition Rule that makes templates a special case. The
only special case I know of is inline functions. Below are parts of the
standard that say something about this issue:
[basic.def.odr] 3.2 One definition rule
1 No translation unit shall contain more than one definition of any
variable, function, class type, enumeration
type or template.
3 Every program shall contain exactly one definition of every non-inline
function or object that is used in that
program; no diagnostic required. The definition can appear explicitly in
the program, it can be found in the
standard or a user-defined library, or (when appropriate) it is
implicitly defined (see 12.1, 12.4 and 12.8).
An inline function shall be defined in every translation unit in which
it is used.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
On Tue, 14 Oct 2003 10:02:56 +0200, "Peter van Merkerk"
<me*****@deadspam.com> wrote: Hm. Looking at the Standard Libraries provided by GCC and STLport, and 'Modern C++ Design' (the only C++ book I own so I don't have any more sources, sorry) seem to place the non-inline definitions in the header files. That's funny, the copy I have of STLport and the Loki library (from 'Modern C++ Design') uses inline implementations for templated (member) functions.
Excerpt from Singleton.h, Loki (under 'fair use', original code by
Alexandrescu & Co: http://cvs.sourceforge.net/viewcvs.p...eton.h?rev=1.2
)
---------------------------------------------------------------------------------------------------------------------------------------
template
<
class T,
template <class> class CreationPolicy,
template <class> class L,
template <class> class M
void SingletonHolder<T, CreationPolicy, L, M>::DestroySingleton()
{
assert(!destroyed_);
CreationPolicy<T>::Destroy(pInstance_);
pInstance_ = 0;
destroyed_ = true;
}
--------------------------------------------------------------------------------------------------------------------------------------------
I can't access my STLport headers from this partition.. I'd say look
at something like std::vector. Most files have one or two non-inline
member functions defined, if any. None of my programs have suffered from me doing this, but I don't know if that's just the compiler or if the standard maybe has some exclusions for templates.
I'm not a language lawyer, but I haven't seen anything in the chapter about the One Definition Rule that makes templates a special case. The only special case I know of is inline functions. Below are parts of the standard that say something about this issue:
[snip]
Yep. I'll definitely need to get the Standard some day now.
"Peter van Merkerk" <me*****@deadspam.com> wrote in message news:<bm************@ID-133164.news.uni-berlin.de>... That would violate the One Definition Rule (discussed in paragraph 3.2 of the standard). If you include the definition in the header file you must use the inline keyword to get around the ODR. If you don't want to do define template functions inline, you need a compiler that supports the export keyword, such as the Comeau compiler (http://www.comeaucomputing.com).
[and] I'm not a language lawyer, but I haven't seen anything in the chapter about the One Definition Rule that makes templates a special case. The only special case I know of is inline functions. Below are parts of the standard that say something about this issue:
[basic.def.odr] 3.2 One definition rule 1 No translation unit shall contain more than one definition of any variable, function, class type, enumeration type or template.
3 Every program shall contain exactly one definition of every non-inline function or object that is used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is used.
Well, the thing when it comes to templates, is that what you provide
IS NOT a definition. It's a template. The compiler will generate the
definition according to the types (and if not defined before with the
same types). So it is not part of the One Definition Rule.
When it comes to template functions && member functions of templates,
the compiler can but put them in linkonce sections --> define them
in many object files and collapse them with the linker. This also
makes sense because the compiler can judge better if a function
should be inlined or not, and "inline" is just a hint.
Btw, "export" is no big deal.
stelios
Lightweight C++ Preprocessor: http://students.ceid.upatras.gr/~sxanth/lwc/index.html This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: mike420 |
last post by:
I think everyone who used Python will agree that its syntax is
the best thing going for it. It is very readable and easy
for everyone to learn. But, Python does not a have very good
macro...
|
by: David MacQuigg |
last post by:
Seems like we need a simple way to extend Python syntax that doesn't
break existing syntax or clash with any other syntax in Python, is
easy to type, easy to read, and is clearly distinct from the...
|
by: muchan |
last post by:
A simple question. (Sorry, if it's already in FAQ)
What is the syntax definition language used in the XML 1.0 page
of the W3C <http://www.w3.org/TR/REC-xml/>?
I searched some syntax definition...
|
by: Sönke Tesch |
last post by:
Hi everybody,
I have a problem with the following piece of code:
141: /* A data block to manage a single log target: */
142: typedef struct {
143: apr_reslist_t *dbs; /* connection pool */...
|
by: C# Learner |
last post by:
Why is C syntax so uneasy on the eye?
In its day, was it _really_ designed by snobby programmers to scare away
potential "n00bs"? If so, and after 50+ years of programming research,
why are...
|
by: Raj Kotaru |
last post by:
Hello all,
I recently came across the following segment of code that defines a C
struct:
typedef struct
{
unsigned char unused_bits:4;
unsigned char wchair_state:2;
} xyz;
|
by: Marcin Grzębski |
last post by:
I red MSDN article of C# 2.0 this week... and i found
very strange syntax for properties e.g.:
public int MyIntValue {
get {
// ...
}
protected set {
// ...
}
|
by: John |
last post by:
Is this a valid C++ program that will not crash on any machine?
#include <iostream>
using namespace std;
int main( void ) {
int i;
cin >i;
double X;
X = 1123;
|
by: janzon |
last post by:
Hi!
When defining a new object m of class MyClass, with no parameters to
the constructor, one is supposed to write "MyClass m;". If one wants to
pass a parameter p to the constructor, one is...
|
by: parag_paul |
last post by:
Hi all
I am seeing the following code in one place
standalone line in some function ,
{
(void*) new (h) Class_Name(xip, virobj, type, true);
}
Does it make h an object of the Class_Name...
|
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...
|
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: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |