dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: using printf in template function

  1. #1
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    using printf in template function

    Hello gurus

    I have a question about using printf
    The following code prints out the element in the container
    Code:
    // sequential containers
        template
        <   typename Ch,
            template <typename CharT = Ch, typename Alloc = std::allocator<Ch> >
            class Container
        >
        inline void
        Print(const Container<Ch>& cont, const char* delim = " ") throw()
        {
            std::set_unexpected(Exceptions::Unexpected);
    
            const Ch* ptr = &(*cont.begin());
            while(ptr != &(*cont.end()))
            {
                std::printf("%d%s", *ptr, delim);
                ++ptr;
            }
        }
    My question is, how do I know which flag to use for primitive types?

    Thanks for the help.

  2. #2
    Join Date
    Aug 2007
    Posts
    858

    Re: using printf in template function

    Why not use streams? With them, the function becomes as simple as

    Code:
    template</*...*/>
    void Print(const Container<Ch>& cont, std::ostream& os, const char* delim = " ")
    {
      std::copy(cont.begin(), cont.end(), std::ostream_iterator<Ch>(os, delim));
    }
    Otherwise I guess you could try to make a template wrapper for printf, with specializations to give the appropriate format string for each type. But that's a lot of work for basically no gain.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,762

    Re: using printf in template function

    What primitive types do you expect? If you really want to use printf for primitive types and these types vary, then you should specialise the function template for them.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: using printf in template function

    thank you Speedo and laserlight

    Is it aslo acceptable to say that using typeid to format the format string produces consistent result?

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: using printf in template function

    A hybrid approach, if you absolutely *must* use printf, would be to output the value using stream syntax to an ostringstream, and then print the stream.str().c_str() to a &#37;s format specifier.

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: using printf in template function

    I didn't think about the hybrid approach and that sounds like an idea.
    Thank you Lindley!

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)