Bug 23053 - Const-correctness issue in TR1 hashtable
Summary: Const-correctness issue in TR1 hashtable
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.0.1
: P3 minor
Target Milestone: 4.0.2
Assignee: Paolo Carlini
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-07-25 02:06 UTC by Dave Odell
Modified: 2005-07-26 09:48 UTC (History)
1 user (show)

See Also:
Host: i486-linux-gnu
Target: i486-linux-gnu
Build: i486-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2005-07-25 08:39:08


Attachments
Preprocessed test case (112 KB) (108.42 KB, application/octet-stream)
2005-07-25 02:08 UTC, Dave Odell
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Dave Odell 2005-07-25 02:06:33 UTC
I get errors when I try to compile the following bit of code:

#include <tr1/unordered_set>

int main()
{
        std::tr1::unordered_set<int> s;

        const std::tr1::unordered_set<int> &s_ref = s;

        s_ref.find(27); // Problem is here.

        return 0;
}

It appears that hashtable::find_node should be marked as a const member 
function and isn't. Putting const in (see below) seems to let the thing work, 
though I haven't tested it rigorously.

debian-800:/usr/include/c++/4.0/tr1# diff hashtable-orig hashtable-fixed
863c863
<   node* find_node (node* p, const key_type& k, typename hashtable::hash_code_t c);
---
>   node* find_node (node* p, const key_type& k, typename hashtable::hash_code_t
c) const;
1219c1219
< ::find_node (node* p, const key_type& k, typename hashtable::hash_code_t code)
---
> ::find_node (node* p, const key_type& k, typename hashtable::hash_code_t code)
const

Here's the full text of the error I get:

david@debian-800:~/c$ g++-4.0 -v -save-temps -fmessage-length=80 temp.cpp
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f95,objc,ada,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib --enable-nls
--without-included-gettext --enable-threads=posix --program-suffix=-4.0
--enable-__cxa_atexit --enable-libstdcxx-allocator=mt --enable-clocale=gnu
--enable-libstdcxx-debug --enable-java-gc=boehm --enable-java-awt=gtk
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0-1.4.2.0/jre --enable-mpfr
--disable-werror --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.0.1 (Debian 4.0.1-2)
 /usr/lib/gcc/i486-linux-gnu/4.0.1/cc1plus -E -quiet -v -D_GNU_SOURCE temp.cpp
-mtune=i486 -fmessage-length=80 -fpch-preprocess -o temp.ii
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i486-linux-gnu/4.0.1/../../../../include/c++/4.0.1
 /usr/lib/gcc/i486-linux-gnu/4.0.1/../../../../include/c++/4.0.1/i486-linux-gnu
 /usr/lib/gcc/i486-linux-gnu/4.0.1/../../../../include/c++/4.0.1/backward
 /usr/local/include
 /usr/lib/gcc/i486-linux-gnu/4.0.1/include
 /usr/include
End of search list.
 /usr/lib/gcc/i486-linux-gnu/4.0.1/cc1plus -fpreprocessed temp.ii -quiet
-dumpbase temp.cpp -mtune=i486 -auxbase temp -version -fmessage-length=80 -o temp.s
GNU C++ version 4.0.1 (Debian 4.0.1-2) (i486-linux-gnu)
        compiled by GNU C version 4.0.1 (Debian 4.0.1-2).
GGC heuristics: --param ggc-min-expand=47 --param ggc-min-heapsize=32148
/usr/lib/gcc/i486-linux-gnu/4.0.1/../../../../include/c++/4.0.1/tr1/hashtable: In
   member function 'typename std::tr1::hashtable<Key, Value, Allocator,
   ExtractKey, Equal, H1, H2, H, RehashPolicy, cache_hash_code,
   mutable_iterators, unique_keys>::const_iterator std::tr1::hashtable<Key,
   Value, Allocator, ExtractKey, Equal, H1, H2, H, RehashPolicy,
   cache_hash_code, mutable_iterators, unique_keys>::find(const Key&) const
   [with Key = int, Value = int, Allocator = std::allocator<int>, ExtractKey =
   Internal::identity<int>, Equal = std::equal_to<int>, H1 =
   std::tr1::hash<int>, H2 = Internal::mod_range_hashing, H =
   Internal::default_ranged_hash, RehashPolicy = Internal::prime_rehash_policy,
   bool cache_hash_code = false, bool mutable_iterators = false, bool
   unique_keys = true]':
temp.cpp:9:   instantiated from here
/usr/lib/gcc/i486-linux-gnu/4.0.1/../../../../include/c++/4.0.1/tr1/hashtable:1135:
error: passing
   'const std::tr1::hashtable<int, int, std::allocator<int>,
   Internal::identity<int>, std::equal_to<int>, std::tr1::hash<int>,
   Internal::mod_range_hashing, Internal::default_ranged_hash,
   Internal::prime_rehash_policy, false, false, true>' as 'this' argument of '
   typename std::tr1::hashtable<Key, Value, Allocator, ExtractKey, Equal, H1,
   H2, H, RehashPolicy, cache_hash_code, mutable_iterators, unique_keys>::node*
   std::tr1::hashtable<Key, Value, Allocator, ExtractKey, Equal, H1, H2, H,
   RehashPolicy, cache_hash_code, mutable_iterators,
   unique_keys>::find_node(Internal::hash_node<Value, cache_hash_code>*, const
   Key&, typename std::tr1::hashtable<Key, Value, Allocator, ExtractKey, Equal,
   H1, H2, H, RehashPolicy, cache_hash_code, mutable_iterators,
   unique_keys>::hash_code_t) [with Key = int, Value = int, Allocator =
   std::allocator<int>, ExtractKey = Internal::identity<int>, Equal =
   std::equal_to<int>, H1 = std::tr1::hash<int>, H2 =
   Internal::mod_range_hashing, H = Internal::default_ranged_hash, RehashPolicy
   = Internal::prime_rehash_policy, bool cache_hash_code = false, bool
   mutable_iterators = false, bool unique_keys = true]' discards qualifiers
Comment 1 Dave Odell 2005-07-25 02:08:45 UTC
Created attachment 9356 [details]
Preprocessed test case (112 KB)
Comment 2 Chris Jefferson 2005-07-25 07:46:17 UTC
Actually according to the TR1 spec (n1745 at least), there should be a non-const version which returns an 
iterator, and a const version which returns a const_iterator.
Comment 3 Chris Jefferson 2005-07-25 07:52:06 UTC
Apologises, I misread the problem. Ignore my previous comment. Yes, I agree that find_node (which is a 
private function) should be const. An identical problem exists calling equal_range
Comment 4 Paolo Carlini 2005-07-25 08:39:08 UTC
Ok, thanks.
Comment 5 CVS Commits 2005-07-25 22:46:56 UTC
Subject: Bug 23053

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	paolo@gcc.gnu.org	2005-07-25 22:46:48

Modified files:
	libstdc++-v3   : ChangeLog 
	libstdc++-v3/include/tr1: hashtable 
Added files:
	libstdc++-v3/testsuite/tr1/6_containers/unordered/hashtable: 
	                                                             23053.cc 

Log message:
	2005-07-25  Dave Odell  <evilalias@hotmail.com>
	
	PR libstdc++/23053
	* include/tr1/hashtable (hashtable<>::find_node): Const-ify.
	* testsuite/tr1/6_containers/unordered/hashtable/23053.cc: New.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/ChangeLog.diff?cvsroot=gcc&r1=1.3063&r2=1.3064
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/tr1/hashtable.diff?cvsroot=gcc&r1=1.4&r2=1.5
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/testsuite/tr1/6_containers/unordered/hashtable/23053.cc.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 6 CVS Commits 2005-07-26 09:46:35 UTC
Subject: Bug 23053

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-4_0-branch
Changes by:	paolo@gcc.gnu.org	2005-07-26 09:46:23

Modified files:
	libstdc++-v3   : ChangeLog 
	libstdc++-v3/include/tr1: hashtable 
Added files:
	libstdc++-v3/testsuite/tr1/6_containers/unordered/hashtable: 
	                                                             23053.cc 

Log message:
	2005-07-26  Dave Odell  <evilalias@hotmail.com>
	
	PR libstdc++/23053
	* include/tr1/hashtable (hashtable<>::find_node): Const-ify.
	* testsuite/tr1/6_containers/unordered/hashtable/23053.cc: New.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.2917.2.69&r2=1.2917.2.70
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/testsuite/tr1/6_containers/unordered/hashtable/23053.cc.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/tr1/hashtable.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.3&r2=1.3.4.1

Comment 7 Paolo Carlini 2005-07-26 09:48:53 UTC
Fixed for 4.0.2.