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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:37:11+00:00 2026-05-30T11:37:11+00:00

I am trying to iterate through an object hierarchy and the object hierarchy is

  • 0

I am trying to iterate through an object hierarchy and the object hierarchy is composed of a known set of classes combined using composition. I would like to build an object model to show the hierarchy / composition graphically. The composition is done based on few rules but it is fluid and flexible.

Quite a few classes (25+) are available and the number of building blocks is increasing. If I search each type in every other type then we have a significantly large number of combinations possible.

I could build a large table where I search for each of the other objects for a given type and recursively build the object model but may be there is a better way and so here I am asking the experts.

Is it possible to know if a function / member variable is present on a particular type at runtime.

My sample code is shown below :

#include <iostream>

struct Generic {};

struct SimpleType {int toString(){return 0;}};

enum ETypeVal{eVal1 = 0, eVal2 = 1, eVal3 = 2};


template <typename ETypeVal val>
struct Hello
{
    int toString(){return 0;}
};

template <> struct Hello<eVal2>
{
    int toString(){return 1;}
};

template <> struct Hello<eVal3>
{   
};


template <class Type>
class TypeHasToString
{
public:
    typedef bool Yes;
    typedef short No;

    static bool const value = (sizeof(HasToString<Type>(0)) == sizeof(Yes));

private:

    template <typename T, T> struct TypeCheck;

    template <typename T> struct ToString
    {
        typedef int (T::*fptr)();
    };

    template <typename T> static Yes HasToString(TypeCheck< typename ToString<T>::fptr, &T::toString >*);
    template <typename T> static No  HasToString(...);
};

int main(int argc, char *argv[])
{
    // all this works fine
    std::cout << TypeHasToString<Generic>::value << std::endl;
    std::cout << TypeHasToString<SimpleType>::value << std::endl;
    std::cout << TypeHasToString<Hello<eVal1>>::value << std::endl;
    std::cout << TypeHasToString<Hello<eVal2>>::value << std::endl;
    std::cout << TypeHasToString<Hello<eVal3>>::value << std::endl;

    // Unable to deduce for type that are not known at compile time
    // Is it possible to remove this limitation ?
    for(int val = eVal1; val <= eVal3; val++)
    {
        std::cout << TypeHasToString< Hello< (ETypeVal)val > >::value << std::endl;
    }

    return 0;
}
  • 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-05-30T11:37:13+00:00Added an answer on May 30, 2026 at 11:37 am

    I’ve used boost::mpl to do the iteration and printing of the values. Most of this should be possible without any of those, but I heavily recommend using it. Also I’ve fixed some things in your code. You might also want to use BOOST_HAS_XXX instead of your homebrew solution (your SFINAE style is rather awkward).

    #include <iostream>
    #include <boost/mpl/vector.hpp>
    #include <boost/mpl/range_c.hpp>
    #include <boost/mpl/transform.hpp>
    #include <boost/mpl/for_each.hpp>
    
    
    struct Generic {};
    
    struct SimpleType {int toString(){return 0;}};
    
    enum ETypeVal{ eVal1 = 0, eVal2 = 1, eVal3 = 2};
    
    template <ETypeVal val>
    struct Hello
    {
      int toString(){return 0;}
    };
    
    template <> struct Hello<eVal2>
    {
      int toString(){return 1;}
    };
    
    template <> struct Hello<eVal3>
    {   
    };
    
    
    template <class Type>
    class TypeHasToString
    {
    public:
      typedef bool Yes;
      typedef short No;
    private:
    
      template <typename T, T> struct TypeCheck;
    
      template <typename T> struct ToString
      {
        typedef int (T::*fptr)();
      };
    
      template <typename T> static Yes HasToString(TypeCheck< typename ToString<T>::fptr, &T::toString >*);
      template <typename T> static No  HasToString(...);
    
    public:
      static bool const value = (sizeof(HasToString<Type>(0)) == sizeof(Yes));
    };
    
    template<typename val>
    struct make_hello { typedef Hello< ETypeVal(val::value)> type; };
    
    struct print_seq {
      template<typename T>
      void operator()(const T&) const {
        std::cout << T::value << std::endl;
      }
    };
    
    int main()
    {
      using namespace boost::mpl;
      // maybe have a last enum here
      typedef range_c<int, eVal1, eVal3 + 1>::type range; 
      // range has no clear so we need the inserter
      typedef transform<range, make_hello<_1>, back_inserter< vector0<> > >::type hellos;
      typedef transform< hellos, TypeHasToString<_1> >::type booleans;
      // namespace for clarity
      boost::mpl::for_each<booleans>( print_seq() );
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to iterate through a set of keys in a properties file, so
Using SQLite3 with Python 2.5, I'm trying to iterate through a list and pull
I am trying to iterate through a JSON object to import data, i.e. title
I'm trying to iterate through a bunch of variables in a javascript (using jQuery)
I'm trying to iterate through some JSON results from the YQL geo.places table using
I'm trying to figure out how to iterate through a JSON object in an
I'm trying to make a PHP (5) object that can iterate through its properties,
I'm trying to iterate through a nested object to retrieve a specific object identified
I am trying to iterate through each of the members of an object. For
I'm trying to find a way to iterate through an enum's values while using

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.