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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:08:38+00:00 2026-06-11T20:08:38+00:00

Is it possible to change this C++11 initialization: const std::map<int, std::map<int, std::string>> test =

  • 0

Is it possible to change this C++11 initialization:

const std::map<int, std::map<int, std::string>> test =
  {{1,
    {{1, "bla"},
     {2, "blie"}
    }
   },
   {3,
    {{1, "ha"},
     {2, "hie"}
    }
   }
  };

To some form with Boost.Assignment without using temporaries? It does not seem possible to nest map_list_of in this way, unfortunately. Am I wrong?

Note: I am prepared for some terrible macros. As long as it works generally enough it would be fine. Variadic templates are not OK, as the target compiler is Intel C++ 2013 and/or MSVS2012.

EDIT: The “ideal” wrapper interface I would like to use looks something like this:

//header
extern const std::map<int, std::map<int, std::string>> test;

// source file
/*something*/ test
  /*something*/ 1,
  /*something*/ 1, "bla" /*something*/
  /*something*/ 2, "blie" /*something*/
  /*something*/ 2 //etc...

Where any /*something*/ can be empty. This should use both C++11 brace init or boost::assign::map_list_of. I am trying to avoid a manual repetition like here: https://stackoverflow.com/a/1872506/256138

  • 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-11T20:08:40+00:00Added an answer on June 11, 2026 at 8:08 pm

    It is possible to nest map_list_of in this manner, with hackery (but there may be temporaries created underneath, I’m unsure of that):

    #include <map>
    #include <string>
    #include <boost/assign/list_of.hpp>
    using boost::assign::map_list_of;
    
    const std::map<int, std::map<int, std::string> > test =
        map_list_of
            (1, map_list_of
                (1, "bla")
                (2, "blie")
                .convert_to_container<std::map<int, std::string> >()
            )
            (3, map_list_of
                (1, "ha")
                (2, "hie")
                .convert_to_container<std::map<int, std::string> >()
            )
        ;
    
    // Correctly prints "hie".
    //std::cout << test.find(3)->second.find(2)->second << "\n";
    

    Possible macro interface (ignoring the empty requirement, mainly because I am unsure what it means):

    #include <iostream>
    #include <string>
    #include <map>
    
    #ifdef CPP_11_AVAILABLE // Unsure what the actual macro is.
    #define map_entries_begin {
    #define map_entries_end }
    #define map_entry_begin {
    #define map_entry_end },
    
    #else
    #include <boost/assign/list_of.hpp>
    #define map_entries_begin boost::assign::map_list_of
    #define map_entries_end
    #define map_entry_begin (
    #define map_entry_end )
    
    #endif
    
    const std::map<int, std::map<int, std::string>> test =
        map_entries_begin
            //------//
            map_entry_begin
                1, map_entries_begin
                     map_entry_begin 1, "bla" map_entry_end
                     map_entry_begin 2, "blie" map_entry_end
                   map_entries_end
            map_entry_end
    
            //------//
            map_entry_begin
                3, map_entries_begin
                     map_entry_begin 1, "ha" map_entry_end
                     map_entry_begin 2, "hie" map_entry_end
                   map_entries_end
            map_entry_end
    
        map_entries_end;
    
    int main()
    {
        std::cout << test.find(3)->second.find(2)->second << "\n";
        return 0;
    }
    

    Admittedly quite verbose but seems to meet your requirement.

    See C++11 online demo at http://ideone.com/6Xx2t.

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

Sidebar

Related Questions

Is it possible to change this code: @Override public boolean onKeyDown(int keyCode, KeyEvent event)
Is this possible to change bundle structure in Symfony 2, for example: MyBundle EntityName
Is it possible to change PowerPacks.LineShape smoothingMode? I tried to use this code(a class
Using JQuery it possible to clone a Div like this and change add a
Is this possible? example: $('a.change').click(function(){ //code to change p tag to h5 tag });
Possible Duplicate: Can you animate a height change on a UITableViewCell when selected? This
Ok, not sure if this is even possible, but I want to change the
I have this: struct myClass{ multiset<string,binPred<string> > values ; myClass(const char param1, const char
Is it possible to change this code, with a return value and an exception:
Is it possible to re-write this query to restart numbering when the UName changes

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.