Syntax for defining non-inline func. in Templates

S

Senthilraja

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

e

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

Senthilraja

e said:
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.
 
P

Peter van Merkerk

I have the following program using templates. Someone please let me
know
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).
 
E

Elven

Peter van Merkerk said:
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?
 
P

Peter van Merkerk

Remember, the full template definition must be present for the
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.
 
E

e

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.py/*checkout*/loki-lib/loki/Borland/Singleton.h?rev=1.2
)

---------------------------------------------------------------------------------------------------------------------------------------
template
<
class T,
template <class> class CreationPolicy,
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.
 
S

stelios xanthakis

Peter van Merkerk said:
[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
 

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