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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:21:30+00:00 2026-06-17T19:21:30+00:00

Here is my design for traversal of a node tree: struct Leaf1{}; struct Leaf2{};

  • 0

Here is my design for traversal of a node tree:

struct Leaf1{};
struct Leaf2{};
struct Leaf3{};
struct Leaf4{};
struct Leaf5{};

typedef boost::variant< Leaf4, Leaf5 > Node3;
typedef boost::variant< Leaf2, Leaf3, Node3> Node2;
typedef boost::variant< Node2, Leaf1 > Node1;

class NodeVisitor: public boost::static_visitor<void>
{
public:
    template<class Node>
    void operator()(const Node& e) const
    {
        boost::apply_visitor( *this, e );
    }

    void operator()(const Leaf1& e) const{}
    void operator()(const Leaf2& e) const{}
    void operator()(const Leaf3& e) const{}
    void operator()(const Leaf4& e) const{}
    void operator()(const Leaf5& e) const{}
};

So I recursively visit the nodes until I arrive at a leaf. The problem above is that I must add a stub for operater() for each leaf. You can see that I have five such stubs above but have many more in practice. Can you suggest a way of templating this stub?

  • 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-17T19:21:31+00:00Added an answer on June 17, 2026 at 7:21 pm

    SOLUTION 1: SFINAE-based technique

    This solution is based on the fact that failing to substitute template parameters during the instantiation of a template does not cause a compilation error (Substitution Failure Is Not An Error): instead, that template is simply disregarded for overload resolution. Thus, with some trick you can select which overloads of a certain function template shall be made visible depending on the template arguments provided at instantiation time.

    When this technique is used, it is important to make sure that the discriminating conditions which decide the visibility of each overload are mutually exclusive, or ambiguity may arise.

    To begin with, you need to define some trait metafunction that helps you determine whether a certain class is a leaf:

    // Primary template
    template<typename T> struct is_leaf<T> { static const bool value = false; };
    
    // Specializations...
    template<> struct is_leaf<Leaf1> { static const bool value = true; };
    template<> struct is_leaf<Leaf2> { static const bool value = true; };
    ...
    

    Then, you can use std::enable_if (or boost::enable_if if you are working with C++98) to select which overload of the call operator should be made visible:

    class NodeVisitor: public boost::static_visitor<void>
    {
    public:
    
        // Based on the fact that boost::variant<> defines a type list called
        // "types", but any other way of detecting whether we are dealing with
        // a variant is OK
        template<typename Node>
        typename std::enable_if<
            !is_same<typename Node::types, void>::value 
            >::type 
        operator()(const Node& e) const
        {
            boost::apply_visitor( *this, e );
        }
    
        // Based on the fact that leaf classes define a static constant value
        // called "isLeaf", but any other way of detecting whether we are dealing
        // with a leaf is OK
        template<typename Leaf>
        typename std::enable_if<is_leaf<Leaf>::value>::type 
        operator()(const Leaf& e) const
        {
            ...
        }
    };
    

    SOLUTION 2: overload-based technique

    If you are working on C++98 and do not want to use boost::enable_if as a replacement for std::enable_if, an alternative approach consists in exploiting overload resolution and an unused argument for discriminating between two overloads of a helper function. First of all, you define two dummy classes:

    struct true_type { };
    struct false_type { };
    

    Then, you create your is_leaf<> metafunction again, properly specializing it for leaf classes:

    // Primary template
    template<typename T> struct is_leaf<T> { typedef false_type type; };
    
    // Specializations...
    template<> struct is_leaf<Leaf1> { typedef true_type type; };
    template<> struct is_leaf<Leaf2> { typedef true_type type; };
    ...
    

    Finally, you create an instance of one of those dummy types to choose the proper overload of a helper function process():

    class NodeVisitor: public boost::static_visitor<void>
    {
    public:
    
        template<typename T>
        void operator()(const T& e) const
        {
            typedef typename is_leaf<T>::type helper;
            process(e, helper());
        }
    
        template<typename Node>
        void process(const Node& e, false_type) const
        {
            boost::apply_visitor(*this, e);
        }
    
        template<typename Leaf>
        void process(const Leaf& e, true_type) const
        {
            ...
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Fluent Interface design here if i call something like dog.Train(Running).Train(Eating).Do(Running).Do(Eating); what is the
Im having a big headcache with a header design: Here the demo: http://jsfiddle.net/uGECm/ The
I have posted a question about multilanguage database design here, [] What are best
oo design basics here... Edit: To clarify after first answer - I'm not asking
Small design question here. I'm trying to develop a calculation app in C#. I
Here is the design for the shot i have posted : <div id=site_content> <div
Question over here is are design patterns specific to a programming language or technology,
There is a big design flaw here, but I'm having trouble solving it: The
We're using the MVP design pattern here, and we've gone with the presenter-per-UserControl style.
Updated Problem solved, I have some design problem here. The directory looks like that:

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.