typename or class?

Discussion in 'C++' started by nietoperz34, May 26, 2012.

  1. nietoperz34

    nietoperz34 Guest

    Welcome. What better use in templates C++? Typename or class?
    Better yes:
    template <class T1, class T2>
    T1 function(T2 argument);
    Or better yes:
    template <typename T1, typename T2>
    T1 function(T2 argument);
     
    nietoperz34, May 26, 2012
    #1
    1. Advertising

  2. On May 26, 4:30 pm, nietoperz34 <> wrote:
    > Welcome. What better use in templates C++? Typename or class?
    > Better yes:
    > template <class T1, class T2>
    > T1 function(T2 argument);
    > Or better yes:
    > template <typename T1, typename T2>
    > T1 function(T2 argument);


    Hi
    Actually from point of programming language usage
    using class and typename in template declaration/definition are same.
    For who likes less typing, class is shorter than typename.
    I believe programmers has some personal convention for using class or
    typename. For example Andrei Alexandrescu wrote in his book
    Modern C++ Design:
    Template parameters are declared with class if they can be only a user-
    defined type, and with
    typename if they can also be a primitive type.
    But, please note: typename has another totally separated usage when
    programming
    templates. For example:
    template<class Container>
    void SomeFunc(Container c)
    {
    typename Container::ElemType e; // You have to use typename
    // ...
    }

    In above line, you can't use 'class' instead of 'typename'

    HTH,
    -- Saeed Amrollahi
     
    Saeed Amrollahi, May 26, 2012
    #2
    1. Advertising

  3. nietoperz34

    nietoperz34 Guest

    W dniu sobota, 26 maja 2012 14:30:44 UTC+2 użytkownik nietoperz34 napisał:
    > Welcome. What better use in templates C++? Typename or class?
    > Better yes:
    > template <class T1, class T2>
    > T1 function(T2 argument);
    > Or better yes:
    > template <typename T1, typename T2>
    > T1 function(T2 argument);


    Hi. I will use class, because shorter. Thanks, nietoperz34.
     
    nietoperz34, May 26, 2012
    #3
    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. Alexander Malkis

    class vs. typename

    Alexander Malkis, Apr 6, 2004, in forum: C++
    Replies:
    7
    Views:
    13,214
    psubramani
    Oct 20, 2012
  2. Replies:
    6
    Views:
    647
    Victor Bazarov
    Oct 20, 2005
  3. Victor Bazarov
    Replies:
    6
    Views:
    324
  4. Kobe
    Replies:
    9
    Views:
    481
    Howard
    Feb 17, 2006
  5. Jakob Bieling

    Q: typename or not typename?

    Jakob Bieling, Mar 14, 2006, in forum: C++
    Replies:
    2
    Views:
    375
    Rolf Magnus
    Mar 14, 2006
Loading...

Share This Page