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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:40:58+00:00 2026-06-09T15:40:58+00:00

I’ve got a few classes with the with a visit function like so: struct

  • 0

I’ve got a few classes with the with a visit function like so:

struct Person {
   std::string name;
   unsigned age;

  template<class Visitor>
  void visit(Visitor& c)
  {
      c("name", name);
      c("age", age);
  }

  template<class Visitor>
  void visit(Visitor& c) const
  {
      c("name", name);
      c("age", age);
  }
};

I have a visitor like:

struct PrintVisitor
{
    PrintVisitor(std::ostream& stream)
        : m_stream(stream)
    {}

    template<class T>
    void operator()(const std::string& name, const T& v)
    {
        m_stream << name << ": " << v << std::endl;
    }

private:
    std::ostream& m_stream;
};

For each visitor I want to define a stream operator:

std::ostream& operator<<(std::ostream& stream, const Person& p)
{
    PrintVisitor printer(stream);
    p.visit(printer);
    return stream;
}

Is it possible to provide a single operator<< that accepts any Visitable class?

(Right now, I’m just experimenting with printing, but I actually want to implement
json serialization, deserialization and perhaps equality and less-than operators.

UPDATE

I used Davids solution:

Base class for CRTP:

template <class T>
struct Visitable {};

All visitable classes inherit from there:

struct Person : Visitable<Person> { ... }

Operators and functions are templated as so, and use a static cast to visit the class:

template<class T> 
std::ostream& operator<<(std::ostream& stream, const Visitable<T>& p) 
{ 
    PrintVisitor printer(stream); 
    static_cast<const T&>(p).visit(printer); 
    return stream; 
}
  • 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-09T15:41:00+00:00Added an answer on June 9, 2026 at 3:41 pm

    The approach that you are taking is similar to how the boost serialization library is implemented, with the difference that in their case they are overloading operator& (binary and, not address-of) to interact with the library. Then the visitors will use a single serialize operation on the type.

    Now, the problem is that I don’t really understand the question:

    Is it possible do not have to implement the operator just once in a templated function?

    Do you mean providing a single operator<< that will take any visitor(serializer) type? If that is the question, and without going into much detail, you could try using inheritance and the CRTP to solve this. Implement the operator as a member function in a templated base of all your visitors that takes the concrete visitor as argument.

    I am not too sure how that would apply to the comparison operators, while operator<< and operator>> look natural for serialization, they would be surprising for any other operation.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
i got an object with contents of html markup in it, for example: string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Specifically, suppose I start with the string string =hello \'i am \' me And
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

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.