[LC++]Why map<string, string&> not acceptable?

Shaul Karl shaulka at bezeqint.net
Thu Apr 4 07:34:08 UTC 2002


Attached a short program and a longer compilation output.
If I understand it correctly then the type

    map<string, string&>

is not acceptable. 

But why? 
In particular, in what circumstances will references to other classes 
instead of string& be accepted?

Compiler version is 3.03:

$ g++-3.0 -v
Reading specs from /usr/lib/gcc-lib/i386-linux/3.0.3/specs
Configured with: ../src/configure -v --enable-languages=c,c++,java,f77,p
roto,objc --prefix=/usr --infodir=/share/info --mandir=/share/man 
--enable-shared --with-gnu-as --with-gnu-ld --with-system-zlib 
--enable-long-long --enable-nls --without-included-gettext 
--disable-checking --enable-threads=posix --enable-java-gc=boehm 
--with-cpp-install-dir=bin --enable-objc-gc i386-linux
Thread model: posix
gcc version 3.0.3
$

$ cat -n main.cc
     1	/*
     2	 * main.cc.
     3	 */
     4	
     5	
     6	#include <iostream>
     7	#include <map>
     8	#include <stdexcept>
     9	#include <string>
    10	
    11	using namespace std;
    12	
    13	int main(void)
    14	try {
    15	    map<string, string> map1;
    16	    map<const string, const string> map2;
    17	    map<string, string&> map3;
    18	    map<const string, const string&> map4;
    19	    return 0;
    20	}
    21	catch(const exception &excp) {
    22	    cerr << excp.what() << '\n';
    23	    throw;
    24	}


$ g++-3.0 -Wall main.cc
/usr/include/g++-v3/bits/stl_map.h: In instantiation of 
`std::map<std::string, std::string&, std::less<std::string>, 
std::allocator<std::pair<const std::string, std::string&> > >':
main.cc:17:   instantiated from here
/usr/include/g++-v3/bits/stl_map.h:155: forming reference to reference 
type
   `std::string&'
/usr/include/g++-v3/bits/stl_pair.h: In instantiation of 
`std::pair<const std::string, std::string&>':
/usr/include/g++-v3/bits/stl_tree.h:123:   instantiated from 
`std::_Rb_tree_node<std::pair<const std::string, std::string&> >'
/usr/include/g++-v3/bits/stl_tree.h:670:   instantiated from `void 
std::_Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc>
::_M_empty_initialize() [with _Key = std::string, _Value = 
std::pair<const std::string, std::string&>, _KeyOfValue = 
std::_Select1st<std::pair<const std::string, std::string&> >, _Compare 
= std::less<std::string>, _Alloc = std::allocator<std::pair<const 
std::string, std::string&> >]'
/usr/include/g++-v3/bits/stl_tree.h:648:   instantiated from 
`std::_Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc>
::_Rb_tree(const _Compare&, typename std::_Rb_tree_base<_Value, _Alloc>
::allocator_type&) [with _Key = std::string, _Value = std::pair<const 
std::string, std::string&>, _KeyOfValue = std::_Select1st<std::pair<cons
t std::string, std::string&> >, _Compare = std::less<std::string>, 
_Alloc = std::allocator<std::pair<const std::string, std::string&> >]'
/usr/include/g++-v3/bits/stl_map.h:115:   instantiated from 
`std::map<_Key, _Tp, _Compare, _Alloc>::map() [with _Key = std::string, 
_Tp = std::string&, _Compare = std::less<std::string>, _Alloc = 
std::allocator<std::pair<const std::string, std::string&> >]'
main.cc:17:   instantiated from here
/usr/include/g++-v3/bits/stl_pair.h:84: forming reference to reference 
type
   `std::string& const'



-- 

    Shaul Karl
    email: shaulka(at-no-spam)bezeqint.net 
           Please substitute (at-no-spam) with an at - @ - character.
           (at-no-spam) is meant for unsolicitate mail senders only.






More information about the tuxCPProgramming mailing list