Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid conversion to non-const reference in C++0x... bug in std::pair?

Tags:

c++

c++11

I have been developing a library that is getting pretty large, and now I am adding some template-based parts that use C++0x features. So I tried to compile my library (which compiled entirely without warnings on the current standard) with the flag -std=c++0x using gcc version 4.4.5 (on Linux). Now I got a huge influx of error messages related to the conversion of "temporaries" variables to non-const references. The problem is, they are not temporary!

Here is a small piece of code that reproduces the error:

#include <iostream>
#include <map>

struct scanner {
  scanner& operator &(std::pair<std::string, int&> i) {
    std::cout << "Enter value for " << i.first << ": ";
    std::cin >> i.second;
    return *this;
  };
};

struct vect {
  int q[3];

  void fill(scanner& aScan) {
    aScan & std::pair<std::string, int&>("q0",q[0]) 
          & std::pair<std::string, int&>("q1",q[1]) 
          & std::pair<std::string, int&>("q2",q[2]); 
  };
};

int main() {
  vect v;
  scanner s;
  v.fill(s);
  return 0;
};

If you compile this with the current standard (no c++0x flag) it will compile and run as expected. However, if you compile it with -std=c++0x, it will throw the following error at compile-time:

/usr/include/c++/4.4/bits/stl_pair.h:94: error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int’

I really can't figure this out. I have looked over the web and SO, but none seem to have this problem. Is it a bug in std::pair? I would really like to know what the problem is.. thank you for any insight you can give.

PS: don't complain about the "quality" or "stupidity" of the code above, it is not real code.. just an example that shows the error.

like image 655
Mikael Persson Avatar asked Dec 18 '25 17:12

Mikael Persson


1 Answers

Your code is not valid C++03, comeau gives (after adding a return statement to op&):

"stl_pair.h", line 44: error: qualifiers dropped in binding reference of
          type "int &" to initializer of type "const int"
    pair(const _T1& __a, const _T2& __b) : first(__a), second(__b) {}
                                                              ^
          detected during instantiation of "std::pair<_T1, _T2>::pair(const
                    _T1 &, const _T2 &) [with _T1=std::string, _T2=int &]" at
                    line 17 of "ComeauTest.c"
...

The problem is a reference inside a pair. If I remember correctly, it is a gcc extension that allows this. Gcc does accept it with -std=gnu++98, which is the default for C++, but with -std=c++98, gcc 4.4.3 gives:

In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66,
                 from /usr/include/c++/4.4/algorithm:61,
                 from PREAMBLE:7:
/usr/include/c++/4.4/bits/stl_pair.h: In instantiation of ‘std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int&>’:
<input>:5:   instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:83: error: forming reference to reference type ‘int&’
...
like image 158
Fred Nurk Avatar answered Dec 21 '25 07:12

Fred Nurk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!