Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7789263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:14:43+00:00 2026-06-01T21:14:43+00:00

I want to create a template function that generates a string representation of an

  • 0

I want to create a template function that generates a string representation of an array of things that have a name() method. The things might be kept by value, or by reference (raw or smart)

        template< typename T>
        struct GetStringRepresentation;

        template< typename T>
        struct GetStringRepresentation< std::vector< std::unique_ptr< T > > >
        {
            inline void ()( const std::vector< std::unique_ptr< T > >& seq, std::string& out )
            {
                size_t size = seq.size();
                for (int i=0; i< size; i++)
                {
                    if (i > 0)
                        out += ", ";
                    out += seq[i]->name();
                }
            }
        };

       template< typename T>
        struct GetStringRepresentation< std::vector< std::shared_ptr< T > > >
        {
            inline void ()( const std::vector< std::shared_ptr< T > >& seq, std::string& out )
            {
                size_t size = seq.size();
                for (int i=0; i< size; i++)
                {
                    if (i > 0)
                        out += ", ";
                    out += seq[i]->name();
                }
            }
        };

        template< typename T>
        struct GetStringRepresentation< std::vector< T* > >
        {
            inline void ()( const std::vector< T* >& seq, std::string& out )
            {
                size_t size = seq.size();
                for (int i=0; i< size; i++)
                {
                    if (i > 0)
                        out += ", ";
                    out += seq[i]->name();
                }
            }
        };

        template< typename T>
        struct GetStringRepresentation< std::vector< T > >
        {
            inline void ()( const std::vector< T >& seq, std::string& out )
            {
                size_t size = seq.size();
                for (int i=0; i< size; i++)
                {
                    if (i > 0)
                        out += ", ";
                    out += seq[i].name();
                }
            }
        };

As you can clearly see, there is a bunch of duplication, specially between the reference specializations. I’m not very up to speed with the best way to do this, i would like to see a better way that removes some, or all of the code duplication

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:14:46+00:00Added an answer on June 1, 2026 at 9:14 pm

    Here’s a tip to get you started: Specialize the print method only:

    template <typename T> void print(T const & x)
    {
        std::cout << printer<T>::print(x) << std::endl;
    }
    
    template <typename T> struct printer
    {
        static std::string print(T const & x) { return x.name(); }
    };
    template <typename U> struct printer<U*>
    {
        static std::string print(U * p) { return p->name(); }
    };
    

    This way you only have to write the loop once, and the printer takes care of the details. You can even abstract this further and make a sort of is_pointer_like trait (everything that supports ->), which in turn you specialize for all the smart pointers:

    printer<T, is_pointer_like<T>::value>::print(x); // etc.
    
    template <typename T, bool> struct printer { /* as before */ }
    
    template <typename T> struct printer<T, true>
    {
        static std::string print(T const & p) { return p->name(); }
    };
    
    // Traits:
    template <typename T> struct is_pointer_like : std::false_type { };
    template <typename U> struct is_pointer_like<U*> : std::true_type { };
    template <typename U> struct is_pointer_like<std::shared_ptr<U>> : std::true_type { };
    

    For a similar idea, see the pretty printer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a Visual Studio Item Template that generates an Item (for
Ok, I have an in template function that get's submitted via method=POST. But I
I have this HTML page, and I want to create a jQuery method that
I want to create a template for a component that is used many times
I want to know if it's possible to create a template function and then
In a mini blog app, I want to create a delete function, so that
I want to create a function that can take different types of iterators which
I have a template that generates images and I am binding it to div.
I want to create a jQuery template(s) that will be used project wide. I
I want to create a template class in C#, for example: public class Foo<T>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.