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

  • SEARCH
  • Home
  • 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 6091819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:22:17+00:00 2026-05-23T12:22:17+00:00

boost::lexical_cast is a great tool but in my application I ran into a limitation

  • 0

boost::lexical_cast is a great tool but in my application I ran into a limitation in string -> bool conversion that is bugging me. I need to convert all strings like "0", "false" and "FALSE" into false and "1", "true" and "TRUE" into true.

boost::lexical_cast only support conversion from/to "0" and "1". So my idea was to write my own conversion function which seems to work fine:

bool str_to_bool(const std::string &str)
{
    if(str == "1" || str == "true" || str == "TRUE")
        return true;
    else if(str == "0" || str == "false" || str == "FALSE")
        return false;
    else
        throw std::runtime_error("Bad cast from std::string to bool!");
}

Now I wan to write a wrapper round boost::lexical_cast and write my own template specializations for it. Here is what I’ve got so far:

template<typename Target, typename Source>
inline Target my_cast(const Source& src)
{
    return boost::lexical_cast<Target>(src);
}

template<>
inline bool my_cast(const std::string& src)
{
    return str_to_bool(src);
}

This works great for integers or std::string but obviously fails for string literals or character pointers:

int main(int argc, char* argv[])
{
    std::cout << my_cast<bool>(1) << std::endl;                    //OK
    std::cout << my_cast<bool>(std::string("true")) << std::endl;  //OK
    std::cout << my_cast<bool>("true") << std::endl;               //Fail! 

    return 0;
}

So I tried to write another specialization for char * but it fails to compile!

//does not compile!
template<>
inline bool my_cast(const char*& src)
{
    return str_to_bool(src);
}

What is the correct way to support both std::string and char *?

EDIT 1: the title was stupid. Fixed it.

EDIT 2: I borrowed a solution from boost itself. Posted as a new answer.

  • 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-05-23T12:22:18+00:00Added an answer on May 23, 2026 at 12:22 pm

    Here is a solution that works. I got the idea from boost::lexical_cast itself:

    template<class T>
    struct array_to_pointer_decay
    {
        typedef T type;
    };
    
    template<class T, std::size_t N>
    struct array_to_pointer_decay<T[N]>
    {
        typedef const T * type;
    };
    
    template<typename Target, typename Source>
    Target my_cast_internal(const Source& s)
    {
        return boost::lexical_cast<Target>(s);
    }
    
    template<>
    inline bool my_cast_internal(const std::string& src)
    {
        return str_to_bool(src);
    }
    
    template<>
    inline bool my_cast_internal(const char* const& src)
    {
        return str_to_bool(src);
    }
    
    template<typename Target, typename Source>
    inline Target my_cast(const Source& s)
    {
        typedef typename array_to_pointer_decay<Source>::type src;
    
        return my_cast_internal<Target, src>(s);
    }
    

    The main challenge is to deal with array types. The array_to_pointer_decay converts any array type to the corresponding pointer type. The rest is easy now.

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

Sidebar

Related Questions

In the answer to this question ovanes states: Please be aware that boost::lexical_cast is
Is it safe to ignore exception of boost::lexical_cast when converting int to std::string ?
Can I make all std::string in my application support Unicode with Boost.Locale? After reading
Compiling the following: // file main.cpp #include <string> #include <boost/lexical_cast.hpp> int main() { boost::lexical_cast<std::string>(
I am trying to use Boost's lexical_cast for my C++ project but am running
This causes problems due to extra whitespace: std::string t(3.14 ); double d = boost::lexical_cast<double>
I use the boost lexical_cast library for parsing text data into numeric values quite
So I casted boost::lexical_cast<std::string>(boost::this_thread::get_id()) this thread id to string. Now I wonder is it
I implemented the following GBDM example: #include <boost/lexical_cast.hpp> #include <gdbm.h> #include <iostream> #include <string>
Boost is a great set of libraries and it really boosts productivity. But debugging

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.