2

VC10 and GCC 4.4 accept the following, while Sun Studio 12 does not:

std::pair<char*, int> p1;
std::pair<char* const, int> p2;
p1 = p2

Sun Studio 12 complains:

Error: Cannot use std::pair<char*const, int> to initialize std::pair<char*, int>.

Any ideas why this is happening and how I can get Sun Studio to ignore this. I am working with a third party library, which would be a pain to rewrite just for this sort of thing.

Nick
  • 5,765
  • 5
  • 27
  • 36
  • 1
    Looks like a broken library implementation... can you dig through the `utility` header and figure out if the templated constructor is broken? – Kerrek SB Dec 20 '11 at 14:52
  • 1
    related: http://stackoverflow.com/questions/3857716/can-the-functors-called-from-algorithms-acting-on-a-map-accept-pairk-v-instea – Cubbi Dec 20 '11 at 16:26

2 Answers2

4

It seems to be a known issue with Sun's std library.

Your best bet may be to convince the author of the code to replace the assignment with:

p1 = std::make_pair(p2.first, p2.second);

Or at construction time:

std::pair<char*, int> p1(p2.first, p2.second);
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
2

Are you making sure to use libstlport rather than libCstd? See: https://stackoverflow.com/a/4481452/196844

This is definitely an error in the STL implementation. Section 20.2.2, Pairs, of the C++98 Standard provides for the template constructor template <class U, class V> pair(const pair<U, V>& p) which initializes members first and second from the corresponding members of p, performing implicit conversions as needed.

Community
  • 1
  • 1
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
  • Good point. Would code built using libCstd link against libraries built using STLPort? – Nick Dec 20 '11 at 15:19
  • 1
    @Nick: I am not sure. I have never tried it. It might link, but your program will very likely crash if the binary layouts of template instantiations are different and objects are passed between the two segments. Also, you may encouter One Definition Rule-related linker errors. – Daniel Trebbien Dec 20 '11 at 15:22