Converting Template Types

This is a discussion on Converting Template Types within the C++ Programming forums, part of the General Programming Boards category; I am using an API that uses integers where the calculations I am doing require floating point precision. The API ...

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Converting Template Types

    I am using an API that uses integers where the calculations I am doing require floating point precision. The API provides a templat'd data structure and I am converting between the two template types to satisfy the API. Is this the reasonable thing to do for the conversion?

    Code:
    template <typename T, typename U> void cut(
    	core::coords<T> & t, 
    	core::coords<U> & u)
    {
    	for all t.coords = (T) u.coords;
    }
    Last edited by Tonto; 03-18-2008 at 10:32 PM.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    You've provided no real information about the "API" (other than a hint that it's some set of templated functions and classes) so no answer is possible.

    As a rough rule, if the "API" is documented as being limited to integral types, there is usually a good reason to assume it will work incorrectly with floating point types. You will need to confirm or deny that suspicion through suitable reading of documentation and/or testing.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Yea the type system won't allow anything but integers. I just require floating point in my calculations that can withstand the truncation to integers. I'm just wondering if there's any peculiar thing like operator () for automatic conversions between types for this sort of deal.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 8
    Last Post: 07-24-2006, 08:14 AM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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