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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:20:10+00:00 2026-06-16T04:20:10+00:00

I’ve recently started using boost mirror for ORM reflection so I can minimize the

  • 0

I’ve recently started using boost mirror for ORM reflection so I can minimize the amount of DB-related boiler-plate code.

One kind of pattern I’ve run into is listed below.

Since Bar‘s code is going to look identical even for Baz, I was wondering, is it possible to collapse this code any further?

It would be nice if the getValues() interface would look the same, but the identical implementation could also live in Foo instead.

#include <iostream>

template< typename T >
struct Foo
{
    static std::ostream& GetValues_Worker( std::ostream& os, T const& t )
    {
        // do boost mirror reflection stuff here
        return os;
    }
};

struct Bar : 
    public Foo<Bar>
{
    // REFACTOR IF POSSIBLE:  this code will always look the same - even for Baz
    std::ostream& 
    getValues( std::ostream& os ) const
    {
        return GetValues_Worker( os, *this );
    }
};

struct Baz : 
    public Foo<Baz>
{
    // REFACTOR IF POSSIBLE:  looks the same as Bar
    std::ostream& 
    getValues( std::ostream& os ) const
    {
        return GetValues_Worker( os, *this );
    }
};

int main( int argc, char* argv[] )
{
    Bar b;
    std::cerr << b.getValues( std::cerr ) << std::endl;
}

ANSWER

It turns out that ecatmur’s answer below works in most cases. In my specific situation, I adapted his solution to my real code, it worked in 2 out of 4 cases. In the two cases where it failed to work, it was a bit beyond the scope of the Mickey-Mouse example that I gave above. The closest explanation that I could find in SO which explains the compile-time errors I was getting is probably this post. The crux of the problem appeared to be related to what was happening inside my Worker code. In the two failed cases, I was doing output streaming of the subclasses’ members based on what I was getting back from runtime reflection results in boost mirror. I think this is turned out to be a case of non-deducible context. I still don’t understand why the solution in those two failed cases works exactly (why using a visitor in the form of a virtual method gets around the issue). Anyhow, I stumbled upon that approach and was trying to reduce the code some more (in those 4 cases), but in two of them, I couldn’t really reduce that code any more without running into the non-deducible context issue.

  • 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-16T04:20:11+00:00Added an answer on June 16, 2026 at 4:20 am

    Bar::getValues can be moved to Foo<Bar> using a CRTP downcast:

    template< typename T >
    struct Foo
    {
        static std::ostream& GetValues_Worker( std::ostream& os, T const& t )
        {
            // do boost mirror reflection stuff here
            return os;
        }
    
        std::ostream& getValues( std::ostream& os ) const
        {
            return GetValues_Worker( os, *static_cast<T const *>(this) );
        }
    };
    

    At this point you might as well combine the two methods:

    template< typename T >
    struct Foo
    {
        std::ostream& getValues( std::ostream& os ) const
        {
            T const &t = *static_cast<T const *>(this);
            // do boost mirror reflection stuff here
            return os;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.