Topic: Const promotion with make_pair


Author: cliffg@my-deja.com
Date: 2000/02/10
Raw View
I cannot find a way to get make_pair to work with inserting a pair on a
map or a multimap using Metrowerks CodeWarrior Pro 5. Some example code:

#include <string>
#include <map>
#include <utility>

int main() {

  std::multimap<std::string, std::string> testMap;
  std::string first("first"), second("second");

  testMap.insert(std::pair<const std::string, std::string>(first,
second)); // compiles fine, as expected
  testMap.insert(std::pair<std::string, std::string>(first,
second)); // syntax error - missing const
  testMap.insert(std::make_pair(first, second)); // syntax error - no
const promotion?

   return 0;
}

I fully understand why the second insert is a syntax error, but cannot
find a way to persuade a const type promotion (if I'm using the right
terms) with the make_pair example above. Is the error legitimate
according to the C++ standard? Or should the <std::string, std::string>
pair object returned from make_pair be promoted to <const std::string,
std::string> to satisfy the insert method? Is there a way to use
make_pair with map / multimap insert that is cleaner or easier than
creating a pair object directly (with a const type for the first
portion)?

As a side note, VC++ does not generate the syntax error with the
make_pair example above.

Cliff




Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: hinnant@anti-spam_metrowerks.com (Howard Hinnant)
Date: 2000/02/10
Raw View
In article <87qj40$hbd$1@nnrp1.deja.com>, cliffg@my-deja.com wrote:

> I cannot find a way to get make_pair to work with inserting a pair on a
> map or a multimap using Metrowerks CodeWarrior Pro 5. Some example code:
>
> #include <string>
> #include <map>
> #include <utility>
>
> int main() {
>
>   std::multimap<std::string, std::string> testMap;
>   std::string first("first"), second("second");
>
>   testMap.insert(std::pair<const std::string, std::string>(first,
> second)); // compiles fine, as expected
>   testMap.insert(std::pair<std::string, std::string>(first,
> second)); // syntax error - missing const
>   testMap.insert(std::make_pair(first, second)); // syntax error - no
> const promotion?
>
>    return 0;
> }
>
> I fully understand why the second insert is a syntax error, but cannot
> find a way to persuade a const type promotion (if I'm using the right
> terms) with the make_pair example above. Is the error legitimate
> according to the C++ standard? Or should the <std::string, std::string>
> pair object returned from make_pair be promoted to <const std::string,
> std::string> to satisfy the insert method? Is there a way to use
> make_pair with map / multimap insert that is cleaner or easier than
> creating a pair object directly (with a const type for the first
> portion)?

This is a bug in the Pro 5 compiler.  It will be fixed in the Pro 6
release.  In the mean time an easy workaround is:

  typedef std::multimap<std::string, std::string> Map;
  testMap.insert(Map::value_type(first, second));

The workaround will continue to work in future releases, and in ports to
other compilers.

Metrowerks apologizes for any inconvenience this bug has caused.

Howard Hinnant
Senior Library and Performance Engineer
Metrowerks

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]