Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Syntax for defining non-inline func. in Templates

Reply
Thread Tools

Syntax for defining non-inline func. in Templates

 
 
Senthilraja
Guest
Posts: n/a
 
      10-13-2003
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.


 
Reply With Quote
 
 
 
 
e
Guest
Posts: n/a
 
      10-13-2003
On Mon, 13 Oct 2003 10:06:58 +0530, Senthilraja
<(E-Mail Removed)> 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>:ush(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.
 
Reply With Quote
 
 
 
 
Senthilraja
Guest
Posts: n/a
 
      10-13-2003
e wrote:

> On Mon, 13 Oct 2003 10:06:58 +0530, Senthilraja
> <(E-Mail Removed)> 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>:ush(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.

 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      10-13-2003
> >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>:ush(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




 
Reply With Quote
 
Elven
Guest
Posts: n/a
 
      10-13-2003
"Peter van Merkerk" <(E-Mail Removed)> 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?
 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      10-14-2003
> > > 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..
An inline function shall be defined in every translation unit in which
it is used.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


 
Reply With Quote
 
e
Guest
Posts: n/a
 
      10-15-2003
On Tue, 14 Oct 2003 10:02:56 +0200, "Peter van Merkerk"
<(E-Mail Removed)> 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>:estroySingleton()
{
assert(!destroyed_);
CreationPolicy<T>:estroy(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.

 
Reply With Quote
 
stelios xanthakis
Guest
Posts: n/a
 
      10-15-2003
"Peter van Merkerk" <(E-Mail Removed)> wrote in message news:<bmgan2$ltkjo$(E-Mail Removed)-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..
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Colon (:) syntax in defining fields in a struct Raj Kotaru C Programming 16 09-03-2004 12:26 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM
CSS - Syntax for defining Links with Classes Ben Amada HTML 8 01-16-2004 06:38 AM



Advertisments