CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Converting from const double* to const float*

    I'm compiling a 3rd-party library which has a function like this:-

    Code:
    void some_func (const double* var)
    {
    	some_other_func (var);
    }
    Unfortunately, some_other_func() expects a const float* rather than the const double* that's getting passed to it. This would maybe work for int types but MSVC isn't happy to do it for double* and float* - and I'm not sure if a simple cast would be safe here.

    Obviously I could create a temporary float and initialise it from *var but I just wondered if there's a more elegant solution (i.e. some alternative type of cast that'd be safe) ?

    [Edit...] I just tried it a simple cast and a reinterpret cast but neither of them seems to work here So am I right to think that a temporary variable is the only reliable solution..?

    Code:
    void some_func (const double* var)
    {
    	const float tmp = (const float)*var; 
    	some_other_func (&tmp);
    }
    Last edited by John E; October 7th, 2016 at 05:38 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Converting from const double* to const float*

    The format representations of float (single precision) and double (double precision) are different - so just casting a pointer to one as a pointer to the other doesn't do the internal format change required - so a cast won't work here. So some_other_func() needs to be passed an address of a float. As & require an l-value from which to obtain the address, you can't just de-reference, cast and then take the address as that is an r-value. So going through a temp variable seems about right - and works!

    Code:
    void somefunc2(const float* cfp)
    {
    	cout << *cfp << endl;
    }
    
    void somefunc(const double* cdp)
    {
    	const float t = *cdp;
    	somefunc2(&t);
    }
    
    
    int main()
    {
    	const double cd = 12.4546;
    
    	somefunc(&cd);
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Converting from const double* to const float*

    PS. is const double* a pointer to a single double value or an array of double values? Same with const float* - is this a pointer to a single float or an array of floats? If a single, why pass as a pointer when there's minimal overhead for copy by value for this type? If an array, then using a temp variable in some_func() won't work.

    PPS If the pointer is to just one value and it has to be passed by pointer(?) then a better type would be const double* const so that the pointer is const as well as the data to which it points. As the pointer is non-const this is what made me think that it's pointing to an array.
    Last edited by 2kaud; October 7th, 2016 at 12:18 PM. Reason: PPS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Converting from const double* to const float*

    Quote Originally Posted by 2kaud View Post
    PS. is const double* a pointer to a single double value or an array of double values? Same with const float*
    Good point. It's a 3rd-party library so I can't be 100% sure. I think I'll just flag this up to the original devs and let them solve it.

    Interestingly, gcc will happily compile this!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Converting from const double* to const float*

    Interestingly, gcc will happily compile this!
    Just because some code compiles.....
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured