template function v.s. template class

This is a discussion on template function v.s. template class within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I am feeling template function is more tricky than template class. For the reason that the compiler will ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    template function v.s. template class

    Hello everyone,


    I am feeling template function is more tricky than template class. For the reason that the compiler will do the matching automatically for template function, but for template class, developer can assign how to match.

    Sometimes compiler is doing mysterious matching rules for template function, which makes us confused. Does anyone have the same senses? :-)

    Example,

    1. for template function

    we define
    Code:
    template <class T> void sort (vector <T>&)
    when we invoke like,

    sort (vector<int>)&, T will automatically matched by compiler to int -- we have no control. Sometimes, how compiler will do the matching is mysterious. :-)

    2. for template class

    developer has full control. For example, when use some template class, developer can assign the type of parameter,

    we define,

    Code:
    template <class T> class Foo
    {
        // ...
    }
    when developer use it, we can explicitly assign the type, like Foo <int> or Foo <bool>.

    Any comments or experiences or even disagreement is welcome.


    thanks in advance,
    George

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    You can call a template function with template parameters. Any parameters you specify have to be compatible with what you actually pass to the function.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How about posting code which demonstrates what you are talking about. And, can't you specify the type explicitly with functions too?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Code:
    template<typename T> void foo(vector<T>& v)
    {
    // Some code here
    }
    
    template<typename T> class MyClass
    {
    // Some declaration here
    }
    
    int main()
    {
    vector<int> myv;
    foo(myv);
    foo<int>(myv);
    MyClass<int> myc;
    }
    They are the same, only a class requires you to pass a type for the template, functions do not. But you can specify the templates for both. Sometimes, the compiler can get confused and ask you to specifically tell it what types you are passing to a function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 09:44 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  5. Data init inside of template class function
    By VirtualAce in forum C++ Programming
    Replies: 1
    Last Post: 04-24-2002, 04:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21