GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Jan-2010, 09:26
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 204
ahbi82 will become famous soon enough

About template functions


Template classes has to be coded only in the header files. Does this rule apply to template functions?

One example i can think of is that it is being used as a static function. Meaning its being use by another function define in the header file.

CPP / C++ / C Code:
/* example.hpp */
#ifndef __EXAMPLE_H__
#define __EXAMPLE_H__

int MyFunc(int _arg);

#endif // __EXAMPLE_H__

CPP / C++ / C Code:
#include "example.hpp"

template<typename T>
T square(const T &_arg)
{
    return _arg * _arg;
}

int MyFunc(int _arg)
{
    return square<int>(_arg);
}

If the static function is not being used, will it still be compiled into obj file? I know it sounds stupid, but i think it wouldn't cause T is not being specified.
  #2  
Old 29-Jan-2010, 11:37
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: about template functions.


Quote:
Originally Posted by ahbi82
...but i think...

The template of a templated function must be known to the compiler wherever you use it (usually you do this by putting the template definition in a header file).

CPP / C++ / C Code:
// square.h
#ifndef _SQUARE_H__
#define _SQUARE_H__

template < typename T > T square(const T & _arg)
{
    return _arg * _arg;
}

#endif

CPP / C++ / C Code:
// myfunc.h
#ifndef _MYFUNC_H__
#define _MYFUNC_H__

int MyFunc(int);

#endif 
CPP / C++ / C Code:
// myfunc.cpp
#include "square.h"
int MyFunc(int _arg)
{
    return square < int >(_arg);
}

CPP / C++ / C Code:
// test2.cpp
#include "myfunc.h"

#include <iostream>
using namespace std;

int main()
{
    int x;
    int y;
    x = 42;
    y = MyFunc(x);
    cout << "x = " << x << ", y = " << y << endl;
    return 0;
}

Compiled with:
Code:
g++ -c -Wall -W -pedantic myfunc.cpp g++ -c -Wall -W -pedantic test2.cpp g++ myfunc.o test2.o -o test2

Output is:
Code:
./test2 x = 42, y = 1764

Note that the main() function doesn't know or care that a templated function is used; it only needs to know the signature of MyFunc. Since MyFunc uses the templated function in its implementation, the template must be known to the compiler when myfunc.cpp is compiled.

Regards,

Dave

Footnote: Your use of the word "static" is inappropriate for this discussion. I think you mean "non-templated function" or some such thing. The keyword "static" has a specific meaning unrelated to this topic.
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Graph Representation Problem Peter_APIIT C++ Forum 1 15-Jan-2009 06:14
Tree container kawafis44 C++ Forum 1 24-Oct-2008 01:53
Need help implementing a stack with linked lists michael.wesolow C++ Forum 4 10-Oct-2008 16:45
Fatal error C1083: Cannot open include file mia C++ Forum 5 14-May-2007 15:35
Template usage - global functions ankakusu C++ Forum 3 18-Dec-2006 15:18

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:08.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.