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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:57:55+00:00 2026-06-03T15:57:55+00:00

I can do this with a template specialization I think, for nestedness of 1,2,3

  • 0

I can do this with a template specialization I think, for nestedness of 1,2,3 (most common cases) by respectively nesting 1,2,3 for loops and referring to the types by their typenames in stl…but for arbitrary depth, without use of the preprocessor, is there a way to do this? Maybe with mpl? Or would I need a preprocessor tool as well? Right now I am doing something like this:

template<typename T, int>
struct MapDump {};

template<typename T >
struct MapDump<T,1>
{
  static void dump(const T& map, string file, string header="")
  {
    if (!header.empty())
      cout << header << endl;

    for (typename T::const_iterator cIt = map.begin();
         cIt != map.end();
         ++cIt)
      cout << cIt->first << "," << cIt->second << endl;
  }
};

template<typename T >
struct MapDump<T,2>
{
  static void dump(const T& map, string file, string header="")
  {
    if (!header.empty())
      cout << header << endl;

    for (typename T::const_iterator it1 = map.begin();
         it1 != map.end();
         ++it1)
      for (typename T::mapped_type::const_iterator it2 = it1->second.begin();
           it2 != it1->second.end();
           ++it2)
        cout << it1->first << "," << it2->first << "," << it2->second << endl;
  }
};

which I can call with, for example:

  map<int, map<int, double> > m;
  m[1][1] = 1.0;
  m[1][2] = 1.0;
  m[2][1] = 2.0;
  m[2][2] = 2.0;

  MapDump< map<int, map<int, double> >, 2 >::dump(m, "test.csv");

(I stripped out the fstream stuff and left std::cout to simplify the sample code here) My question is, how can I go about specializing when, say, the last level mapped_type is a container type? For example map > is technically a 2-depth construct and not a one level construct…but my nest of 2 specialization wouldn’t compile for that type…any other suggestions on how to, perhaps abstract thsi further (figure out the depth of the construct at compile time as well) are welcome..thanks!

  • 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-03T15:57:56+00:00Added an answer on June 3, 2026 at 3:57 pm

    This performs the recursion over all nested types until it reaches a
    non-nested type. It uses SFINAE to detect if there is a mapped_type
    member typedef (You can use BOOST_HAS_XXX to create such a helper).

    What it does not yet do is do collect the key values and pass them on
    to the next level. You can either collect keys in a vector and keep
    passing them down or figure out the nesting depth and use an
    approximate tuple (this increases compile time complexity to n^2).

    Do not use decltype and the for_each loop, if you want C++03
    compatibility.

    #include <map>
    #include <iostream>
    
    // sfinae to detect a mapped type
    template<typename T>
    struct has_mapped_type
    { 
    private:
      typedef char one;
      typedef struct { char arr[2]; } two;
      template<typename U>
      struct wrap {};
    
      template<typename U>
      static one test(wrap<typename U::mapped_type>*);
    
      template<typename U>
      static two test(...);
    public:
      static const bool value = sizeof(test<T>(0)) == 1;
    };
    
    
    template<typename T, bool has_mapped_type>
    // false version
    struct dump_impl {
      void operator()(const T& t) const {
        std::cout << t << std::endl;
      }
    };
    
    template<typename T>
    // true version
    struct dump_impl<T, true> 
      : dump_impl<
        typename T::mapped_type
        , has_mapped_type<typename T::mapped_type>::value
      > 
    {
      void operator()(const T& t) const {
        for(auto& x : t) { 
          dump_impl<
            typename T::mapped_type
            , has_mapped_type<typename T::mapped_type>::value
            >::
            operator()(x.second);
        }
      }
    };
    
    template<typename T>
    struct dump : public dump_impl<T, has_mapped_type<T>::value> {
      void operator()(const T& t) const {
        dump_impl<T, has_mapped_type<T>::value>::operator()(t);
      }
    };
    
    int main()
    {
      std::map<int, std::map<int, double> > m;
      m[1][1] = 1.0;
      m[1][2] = 1.0;
      m[2][1] = 2.0;
      m[2][2] = 2.0;
    
      dump<decltype(m)>()(m);
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't figure out what I'm doing wrong in this template. Here's my data:
take look at this: http://www.templatemonster.com/demo/35403.html In this template you can click on the image
This is driving me nuts. I can't seem to get the data template within
The usual definition for a specialization of a template function is something like this:
132 a:4:{s:8:template;a:1:{s:10:index.html;b:1;}s:9:timestamp;i:1256373019;s:7:expires;i:1256373079;s:13:cache_serials;a:0:{}}<body> php<br > java<br > c++<br > </body> Can someone explain this part:
Can this be done? It seems like this should be possible. In general, I
Can this be done in JavaScript? type == 1 ? function1() : function2();
Can this be simplified to a one liner? Feel free to completely rewrite it
Can this method work? public String sayHello(){ return Hello.jsp?name= + laala; } I am
Can this <%= link_to 'Item', {:controller => items, :action => update, :id => @item,

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.