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 8530033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:09:53+00:00 2026-06-11T09:09:53+00:00

Given a function such as: template< typename T > void function1( const T &t

  • 0

Given a function such as:

template< typename T >
void function1( const T &t )
{
  function2( boost::lexical_cast<std::string>(t) );
}

What kind of overhead is incurred if the type passed to function1 is already a std::string?

Does the overhead vary, depending on the type I’m lexical_cast-ing to?

Is it superfluous to make an overloaded function to bypass the cast? E.g.:

void function1( const std::string &t )
{
  function2( t );
}

template< typename T >
void function1( const T &t )
{
  function1( boost::lexical_cast<std::string>(t) );
}

The version of boost may be relevent to your answer, as I understand that lexical_cast has received a few optimizations across revisions.

  • 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-11T09:09:55+00:00Added an answer on June 11, 2026 at 9:09 am

    Since the documentation doesn’t offer anything on this topic, I dug into the lexical_cast source (1.51.0) and found that it does some compile-time checking on the types and decides a specific “caster class” that does the conversion. In case source and target are the same, this “caster class” will simply return the input.

    Pseudo-codified and simplified from source (boost/lexical_cast.hpp:2268):

    template <typename Target, typename Source>
    Target lexical_cast(const Source &arg)
    {
        static if( is_character_type_to_character_type<Target, src> ||
                   is_char_array_to_stdstring<Target, src> ||
                   is_same_and_stdstring<Target, src> )
        //         ^-- optimization for std::string to std::string and similar stuff
        {
          return arg;
        }
        else
        {
          /* some complicated stuff */
        }
    }
    

    I can’t directly see any optimizations for other identity casts, though, and looking through the normally selected lexical_cast_do_cast “caster class” is making my head hurt. 🙁

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

Sidebar

Related Questions

Given: template<typename T> class A { B b; std::vector<T> vec1; std::vector<T> vec2; } I'd
Given a table-valued function such as dbo.Split() from T-SQL: Opposite to string concatenation -
Given a template class as such: template <typename TYPE> class SomeClass { public: typedef
Given a set of functions, such as: template<class A1> Void Go(A1 a); template<class A1,
Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void
I have a member function of a template class declared as such: template <class
Given: (function() { function Foo() { } $.extend(Foo.prototype, { bar: 'hasBeer' }); }) Is
Please tell me what will the call to given function return and how? The
Given a function, how to save it to an R script (.R)? Save works
Given a function y = f(A,X): unsigned long F(unsigned long A, unsigned long x)

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.