3
std::vector< std::pair< const QTextCharFormat, std::vector< std::tr1::regex > > > foo;
std::vector< std::pair< const QTextCharFormat, std::vector< std::tr1::regex > > > bar;

Won't work on gcc 4.6.3 because I cannot call: bar.push_back( std::make_pair( foo.first, foo.second ) ); This compiles and runs fine on Visual Studio, but under gcc I get:

/usr/include/c++/4.6/bits/stl_pair.h:156:2: error: passing ‘const QTextCharFormat’ as ‘this’ argument of ‘QTextCharFormat& QTextCharFormat::operator=(const QTextCharFormat&)’ discards qualifiers [-fpermissive]

Is there an intermediate that Visual Studio is skipping that gets created under gcc?

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • It would probably help if you posted a bit more about the definitions of `foo` and `bar` in the example you say doesn't compile. (I presume they aren't the vectors in your first lines!) The error looks a lot like QTextCharFormat::operator= is being passed an rvalue which can't be converted into a const reference. But it's hard to know more without the rest of the question... – Rupert Swarbrick Apr 18 '14 at 20:16
  • Arg, no they are `std::vectors`, I just messed up on my call. That's fixed now. (Incidentally this works if they are not `std::vectors` somehow trying to push them into the `std::vector` creates the problem.) – Jonathan Mee Apr 21 '14 at 11:08
  • 1
    I think it is simplest to just omit the `const` here. The objects in a vector must be assignable. – M.M Apr 21 '14 at 11:11

2 Answers2

1

Well, bar is an std::vector<std::pair<...>>, therefore trying to assign an std::pair (from std::make_pair) to it is going to fail. What you probably want is to push back that new std::pair into bar.

foo is also an std::vector, therefore you need to select an element and only then call .first or .second:

bar.emplace_back(foo[i].first, foo[i].second);

But from the way you are using it you most likely mistakenly added an std::vector too much in the definitions of both bar and foo.

Shoe
  • 74,840
  • 36
  • 166
  • 272
1

From this answer:

Items in a vector must be assignable. const objects aren't assignable, so attempting to store them in a vector will fail (or at least can fail -- the code is invalid, but a compiler is free to accept it anyway, if it so chooses, though most programmers would generally prefer that invalid code be rejected).

Community
  • 1
  • 1
default
  • 2,637
  • 21
  • 44
  • So it sounds like the problem is that `std::pair< const X, Y >` is not assignable under gcc, but is assignable under Visual Studio. – Jonathan Mee Apr 22 '14 at 11:47