Jump to content
Tuts 4 You

emplace syntax - error fixing:


CodeExplorer

Recommended Posts

CodeExplorer

emplace syntax - error fixing
Having this original method:
 

Quote

 

    // store parsed options from src into dest
    // arguments are appended, not overwritten
    // thus for options that can be given once the first stored argument is used
    // and for options that can be given multiple times the argument lists are concatenated
    inline void store(const variables_map& src, variables_map& dest)
    {
        // append values in src to dest
        for (auto& l_p : src)
        {
            if (dest.count(l_p.first))
                for (auto& s : l_p.second.values())
                    dest[l_p.first]._add(s);
            else
                dest.emplace(l_p);
        }
        for (auto& s : src.unrecognized)
            dest.unrecognized.emplace_back(s);
        for (auto& s : src.positional)
            dest.positional.emplace_back(s);
    }

 

 

 

Source code location: https://github.com/cr-marcstevens/sha1_gpu_nearcollisionattacks/blob/master/contrib/program_options.hpp

I am trying this:

    inline void store(const variables_map& src, variables_map& dest)
    {
        // append values in src to dest
        
        // for (po::variables_map::iterator it = vm.begin(); it != vm.end(); it++) {
        // for (auto& l_p : src)
        for (program_options::variables_map::const_iterator l_p = src.begin();
            l_p != src.end(); l_p++)
        {

            //            for each (std::string s in l_p.second.values())
            //{

            if (dest.count(l_p->first))
                //for (auto& s : l_p.second.values())
                  for each (std::string s in l_p->second.values())
                    dest[l_p->first]._add(s);

            // emplace is causing all problems:
            else
                dest.emplace(l_p);

        }
        //for (auto& s : src.unrecognized)
        for each (std::string s in src.unrecognized)
            dest.unrecognized.emplace_back(s);
        //for (auto& s : src.positional)
        for each (detail::parser s in src.positional)
            dest.positional.emplace_back(s);


    }

emplace:
http://www.cplusplus.com/reference/vector/vector/emplace/

variables_map is defined as fallows:
   

Quote

/* stores a map of parsed option.name => parser, as well as unrecognized options, positional arguments */
    struct variables_map
        : public std::map<std::string, detail::parser>
    {
        std::vector<std::string> unrecognized;
        std::vector<detail::parser> positional;
    };
    //using parsed_options = variables_map;

So any help with this emplace would be great.
 

Edited by CodeExplorer
Link to comment
CodeExplorer

I think I got it!!!:

		std::pair <std::string, program_options::detail::parser> NewPair;
		//NewPair = std::make_pair(std::string("lightbulbs"), detail::parser());
		// for creating instance don't use new!
          
        	// emplace is causing all problems:
			else
			{
				NewPair = std::make_pair(l_p->first, l_p->second);
				dest.emplace(NewPair);
			}

Let me know if I am I right!!!
More info:
http://www.cplusplus.com/reference/utility/pair/pair/

https://stackoverflow.com/questions/4207346/how-can-i-traverse-iterate-an-stl-map

 

Link to comment

What's the error you are getting?

The way the original is written looks like it should work fine since the two types for src and dest are the same. So emplace should be taking the l_p entry no issues.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...