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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:54:38+00:00 2026-06-10T08:54:38+00:00

I feel like I’m having a serious ‘Doh!’ moment here… I’m currently trying to

  • 0

I feel like I’m having a serious ‘Doh!’ moment here…

I’m currently trying to implement:

std::ostream& operator<<(std::ostream &out, const MyType &type)

Where MyType holds a boost::variant of int, char and bool. IE: Make my variant streamable.

I tried doing this:

out << boost::apply_visitor(MyTypePrintVisitor(), type);
return out;

And MyTypePrintVisitor has a templated function that uses boost::lexical_cast to convert the int, char or bool to a string.

However, this doesn’t compile, with the error that apply_visitor is not a function of MyType.

I then did this:

if(type.variant.type() == int)
out << boost::get<int> (type.variant);
// So on for char and bool
...

Is there a more elegant solution I’m missing?
Thanks.

Edit: Problem solved. See the first solution and my comment to that.

  • 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-10T08:54:39+00:00Added an answer on June 10, 2026 at 8:54 am

    You should be able to stream a variant if all its contained types are streamable. Demonstration:

    #include <boost/variant.hpp>
    #include <iostream>
    #include <iomanip>
    
    struct MyType
    {
        boost::variant<int, char, bool> v;
    };
    
    std::ostream& operator<<(std::ostream &out, const MyType &type)
    {
        out << type.v;
    }
    
    int main()
    {
        MyType t;
        t.v = 42;
        std::cout << "int: " << t << std::endl;
    
        t.v = 'X';
        std::cout << "char: " << t << std::endl;
    
        t.v = true;
        std::cout << std::boolalpha << "bool: " << t << std::endl;
    }
    

    Output:

    int: 42
    char: X
    bool: true
    

    If you do need to use a visitor (perhaps because some of the contained types aren’t streamable), then you need to apply it to the variant itself; your snippet of code looks like it’s applying it to a MyType object instead.

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

Sidebar

Related Questions

I feel like there is something obvious I'm missing here. I am trying to
I feel like I've only ever seen this here on SO, but I can't
Feel like I'm going a bit nutty here. I have a detail view with
I feel like I might be missing something here, but something is telling me
Feel like I'm overlooking the obvious here... I've got several vars set up like
I feel like a real noob asking this, but here's my problem: I want
I feel like a total goose for having to ask this, it is the
I feel like this is a simple problem I'm having due to my misunderstanding
Feel like I'm missing something simple here - When I submit this form it
I feel like I'm missing something obvious here. I just wanted to try putting

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.