开发者

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

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 t开发者_C百科emporary!

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.


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&’
...


There are LOTS of bugs in gcc C++0x support, it's very much unfinished and development is ongoing. This is doubly true for a version as old as gcc-4.4.5. If you're serious about starting C++0x development before the standard is ratified, you need to use the bleeding-edge version of the compiler and standard library.


It compiles fine both with and without -std=c++0x with GCC 4.5.2.

I guess GCC 4.4.5 doesn't support C++0x that much to get this working.


Except for the missing return *this; in scanner& operator &(std::pair<std::string, int&> i), your code is valid C++0x.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜