Syntax for defining non-inline func. in Templates

Discussion in 'C++' started by Senthilraja, Oct 13, 2003.

  1. Senthilraja

    Senthilraja Guest

    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.
    Senthilraja, Oct 13, 2003
    #1
    1. Advertising

  2. Senthilraja

    e Guest

    On Mon, 13 Oct 2003 10:06:58 +0530, Senthilraja
    <> 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, Oct 13, 2003
    #2
    1. Advertising

  3. Senthilraja

    Senthilraja Guest

    e wrote:

    > On Mon, 13 Oct 2003 10:06:58 +0530, Senthilraja
    > <> 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.
    Senthilraja, Oct 13, 2003
    #3
  4. > >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, Oct 13, 2003
    #4
  5. Senthilraja

    Elven Guest

    "Peter van Merkerk" <> 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?
    Elven, Oct 13, 2003
    #5
  6. > > > 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
    Peter van Merkerk, Oct 14, 2003
    #6
  7. Senthilraja

    e Guest

    On Tue, 14 Oct 2003 10:02:56 +0200, "Peter van Merkerk"
    <> 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.py/*checkout*/loki-lib/loki/Borland/Singleton.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.
    e, Oct 15, 2003
    #7
  8. "Peter van Merkerk" <> wrote in message news:<bmgan2$ltkjo$-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
    stelios xanthakis, Oct 15, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CRON
    Replies:
    24
    Views:
    200,443
    Adrienne Boswell
    Jun 20, 2006
  2. Johnny
    Replies:
    3
    Views:
    443
    Robert Kern
    Aug 23, 2005
  3. Hari Sekhon
    Replies:
    0
    Views:
    493
    Hari Sekhon
    Jun 20, 2006
  4. Alf P. Steinbach
    Replies:
    7
    Views:
    599
    Paul Bibbings
    May 4, 2010
  5. Öö Tiib
    Replies:
    0
    Views:
    643
    Öö Tiib
    May 4, 2010
Loading...

Share This Page