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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:19:37+00:00 2026-06-13T22:19:37+00:00

I have declared a boost::variant which accepts three types: string , bool and int

  • 0

I have declared a boost::variant which accepts three types: string, bool and int. The following code is showing that my variant accepts const char* and converts it to bool. Is it a normal behavior for boost::variant to accept and convert types not on its list?

#include <iostream>
#include "boost/variant/variant.hpp"
#include "boost/variant/apply_visitor.hpp"

using namespace std;
using namespace boost;

typedef variant<string, bool, int> MyVariant;

class TestVariant
    : public boost::static_visitor<>
{
public:
    void operator()(string &v) const
    {
        cout << "type: string -> " << v << endl;
    }
    template<typename U>
    void operator()(U &v)const
    {
        cout << "type: other -> " << v << endl;
    }
};

int main(int argc, char **argv) 
{
    MyVariant s1 = "some string";
    apply_visitor(TestVariant(), s1);

    MyVariant s2 = string("some string");
    apply_visitor(TestVariant(), s2);

    return 0;
}

output:

type: other -> 1
type: string -> some string

If I remove the bool type from MyVariant and change it to this:

typedef variant<string, int> MyVariant;

const char* is no more converted to bool. This time it’s converted to string and this is the new output:

type: string -> some string
type: string -> some string

This indicates that variant tries to convert other types first to bool and then to string. If the type conversion is something inevitable and should always happen, is there any way to give conversion to string a higher priority?

  • 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-13T22:19:39+00:00Added an answer on June 13, 2026 at 10:19 pm

    I don’t think this is anything particularly to do with boost::variant, it’s about which constructor gets selected by overload resolution. The same thing happens with an overloaded function:

    #include <iostream>
    #include <string>
    
    void foo(bool) {
        std::cout << "bool\n";
    }
    
    void foo(std::string) {
        std::cout << "string\n";
    }
    
    int main() {
        foo("hi");
    }
    

    output:

    bool
    

    I don’t know of a way to change what constructors a Variant has [edit: as James says, you can write another class that uses the Variant in its implementation. Then you can provide a const char* constructor that does the right thing.]

    Maybe you could change the types in the Variant. Another overloading example:

    struct MyBool {
        bool val;
        explicit MyBool(bool val) : val(val) {}
    };
    
    void bar(MyBool) {
        std::cout << "bool\n";
    }
    
    void bar(const std::string &) {
        std::cout << "string\n";
    }
    
    int main() {
        bar("hi");
    }
    

    output:

    string
    

    Unfortunately now you have to write bar(MyBool(true)) instead of foo(true). Even worse in the case of your variant with string/bool/int, if you just change it to a variant of string/MyBool/int then MyVariant(true) would call the int constructor.

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

Sidebar

Related Questions

I have the following variant from the boost lib: typedef boost::variant<int, float, double, long,
I have the following code: #include <string> #include <boost/thread/tss.hpp> static boost::thread_specific_ptr<string> _tssThreadNameSptr; I get
I have declared three variables namely TemplateData tData; TaskInstance tInstance;int tID; in my program.
I have the following code which compiles and runs fine under Visual Studio 2008
I have the following code: #include <boost/smart_ptr/shared_ptr.hpp> #include <boost/numeric/interval.hpp> #include <boost/math/distributions/students_t.hpp> using namespace boost::numeric;
I have declared a variable like this @{ int i = 1; } Now,
I have declared the following type KEY = (IPv4, Integer) type TPSQ = TVar
I have declared two string buffers like, private StringBuffer buf = new StringBuffer(1024); private
I have declared an enum in my server code, and would like my client
I've defined some signal: typedef boost::signals2::signal<void (int temp)> SomeSig; typedef SomeSig::slot_type SomeSigType; I have

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.