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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:06:51+00:00 2026-06-17T11:06:51+00:00

I was working with boost::variant<int,std::string,bool> and its visitors when I runned into an unexpected

  • 0

I was working with boost::variant<int,std::string,bool> and its visitors when I runned into an unexpected behavior: the string and bool values were comparable. I don’t know, why does it work like this, but I found it interesting. My only idea is that the variant with the bool value was interpreted as a char? Someone could explain it to me?
The comparsion visitor:

#include <iostream>
#include <algorithm>
#include <vector>
#include <boost/variant.hpp>
#include <boost/function.hpp>

struct my_less : boost::static_visitor<bool*>
{
   template<typename T>
   bool* operator()(T a, T b) const
   {
       return a<b ? new bool(true) : new bool(false);
   }

   template<typename T, typename U>
   bool* operator()(T a, U b) const
   {
       return NULL;
   }
};

int main()
{
typedef boost::variant<int,bool,std::string> datatype;
datatype *a = new datatype(false);
datatype *b = new datatype("abc");

my_less cmp;

bool* val = boost::apply_visitor(cmp,*a,*b);

if(val)
{
    std::cout << *val;
}
else
{
    std::cout << "NULL";
}

}

EDIT
Here is an extended main function with some test cases:

void show_result(bool* val)
{
if(val)
{
    std::cout << *val << std::endl;
}
else
{
    std::cout << "NULL" << std::endl;
}
}

int main()
{
//std::string a = "bbb";
//bool b = true;
//std::cout << b<a;      //compilation error

typedef boost::variant<int,bool,std::string> datatype;
datatype int_value_1(4);
datatype int_value_2(3);
datatype string_value("abc");
datatype bool_value(true);
my_less cmp;

std::cout<<"First result, compare ints 4 and 3:"<<std::endl;
bool* val = boost::apply_visitor(cmp,int_value_1,int_value_2);
show_result(val);

std::cout<<"Second result, compare int to string 4 to abc " << std::endl;
val = boost::apply_visitor(cmp,int_value_1,string_value);
show_result(val);

std::cout <<"Third result, int 4 to bool true:" << std::endl;
val = boost::apply_visitor(cmp,int_value_1,bool_value);
show_result(val);

std::cout<<"Fourth result, string abc to bool true" << std::endl;
val = boost::apply_visitor(cmp,string_value,bool_value);
show_result(val);

}

The output:

First result, compare ints 4 and 3:
0
Second result, compare int to string 4 to abc
NULL
Third result, int 4 to bool true:
NULL
Fourth result, string abc to bool true
0
  • 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-17T11:06:53+00:00Added an answer on June 17, 2026 at 11:06 am

    OK, now that you’ve completely changed your program, let me try again.

    The problem is:

    datatype *b = new datatype("abc");
    

    "abc" is a const char*, not a std::string. If you want to create a std::string variant, you need to do so explicitly. Otherwise, you’ll end up creating a bool variant because all pointers are convertible to bool, including const char* pointers.

    Try this

    datatype *b = new datatype(std::string("abc"));
    

    This interaction between bool and std::string is apparently well-known and somewhat irritating. boost::variant provides a templated constructor, but the resolution rules prefer the built-in converstion to bool and there’s no way in C++ to specify a template specialization on a constructor. It is possible to explicitly specialize assignment, so you can write:

    datatype b;
    b.operator=<std::string>("abc");
    

    which might be marginally more efficient but much less readable than

    datatype b;
    b = std::string("abc");
    

    If you don’t include bool as a variant, then string literals do automatically convert to std::string. Maybe it’s possible to use some sort of proxy pseudo-boolean class. I’ve never tried.

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

Sidebar

Related Questions

struct A { std::string get_string(); }; struct B { int value; }; typedef boost::variant<A,B>
Why is this boost::lambda expression not working? boost::function<bool (boost::uint64_t, boost::uint64_t&, unsigned int, float)> myFunct
i made a server via boost networking and its working good but when i
I've got a working callback system that uses boost::signal. I'm extending it into a
I'm working with boost threads and I need to reference a thread from its
I have implemented one matrix multiplication with boost::numeric::ublas::matrix (see my full, working boost code
I'm working on a Boost Spirit 2.0 based parser for a small subset of
I'm trying to get the Boost library working in my C++ projects in Eclipse.
I'm working on building the Boost platform for a currently unsupported toolchain. Obviously we'd
I am working with the new version of boost 1.42 and I want to

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.