Thread: explanation regarding Template arguments

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    32

    explanation regarding Template arguments

    hi
    i have a working code which uses templates.But the number of template arguments mismatch with the call to template object. please explain the code below.

    In the below statement one argument is passed to vmmul(ie,<0> ).
    Code:
     // Perform element-wise multiply for each pulse.
    	tmp = vmmul<0>(replica, tmp);
    but in the actual template definition the number of template arguments vary.The vmmul template definition is below.

    Code:
    template <dimension_type Dim,
    typename T0,
    typename T1,
    typename Block0,
    typename Block1>
    typename vsip::impl::Vmmul_traits<Dim, T0, T1, Block0, Block1>::view_type
    vmmul(
    		const_Vector<T0, Block0> v,
    		const_Matrix<T1, Block1> m)
    VSIP_NOTHROW
    {
    	typedef impl::Vmmul_traits<Dim, T0, T1, Block0, Block1> traits;
    	typedef typename traits::block_type block_type;
    	typedef typename traits::view_type view_type;
    
    	return view_type(block_type(v.block(), m.block()));
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well obviously, unless the function has been specialized somewhere else, you will need to supply 6 template arguments to the function...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Apparently, everything else can be deduced from what you passed into the function.

    With template functions you don't need to specify all the types between <> if the rest of them can be deduced by the compiler.
    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
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Anon's right, of course. I should know better than to post before I've had my coffee.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    Thanks for reply
    But there is no default values specified at the template arguments

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by babu198649 View Post
    Thanks for reply
    But there is no default values specified at the template arguments
    No, but the compiler "can figure out" what those values should be from what you have passed. Don't ask me how that works, but that's what happens.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    Is it compulsary to provide atleast one template argument.(Because without any template arguments i get error).
    Also without creating the object arguments are passed to the constructor,how could that happen.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by babu198649 View Post
    Is it compulsary to provide atleast one template argument.(Because without any template arguments i get error).
    Also without creating the object arguments are passed to the constructor,how could that happen.
    It can only deduce the template arguments from the function arguments. Since "Dim" isn't used in the function arguments, you must specify it explicitly.

    What you send to the constructor of the arguments passed as parameters v and m has no bearing on the function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    Thank u king mir,
    That solved all my doubts(I was thiniking too much about class templates ,that i forgot to see that vmmul is actually a function template).

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, 01:17 AM
  2. Deducing Function Template Arguments
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2008, 07:29 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM