Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is this correct C++ code (Boost pool allocator with "const std::string" key)

175 views
Skip to first unread message

Roman W

unread,
May 8, 2012, 2:22:53 PM5/8/12
to
{ reformatted; please limit your lines to 70 characters -mod }

The following code compiles under g++ 4.5.3, but does not under MS
Visual Studio 2008:

#include <map>
#include <boost/pool/pool_alloc.hpp>

int main()
{
typedef const std::string key;
typedef double* (*value)(const int&);
std::map<key, value, std::less<key>,
boost::fast_pool_allocator<std::pair<const key, value> > >
map_with_boost_allocator; // fails
}

(Boost version is 1.48). I've tracked the error to the compiler
thinking that these two lines in boost/pool/pool_allocator.hpp define
the same function:

static pointer address(reference r)
{
return &r;
}
static const_pointer address(const_reference s)
{ return &s; }

Where "reference" and "const reference" are defined as

typedef value_type & reference;
typedef const value_type & const_reference;

In this case, "value_type" is "const K" where "K" == "const
std::string". Which compiler is correct:
g++ in accepting the code or MS Visual Studio 2008 in rejecting the
code?

Regards,
Roman


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Frank Birbacher

unread,
May 16, 2012, 3:21:36 AM5/16/12
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

Am 08.05.12 20:22, schrieb Roman W:
> { reformatted; please limit your lines to 70 characters -mod }


> The following code compiles under g++ 4.5.3, but does not under MS
> Visual Studio 2008:
[snip]
> Where "reference" and "const reference" are defined as
>
> typedef value_type & reference; typedef const value_type &
> const_reference;
>
> In this case, "value_type" is "const K" where "K" == "const
> std::string".

I think this code should be rejected. "const int&" and "const const
int&" are the same type. Thus the function signature is also the same
which leads to an error.

It's up to boost to check your usecase and fix the boost code
accordingly. You might want to open an issue on their bug tracker.

// rejected by comeau online 4.3.10.1 Beta2:
// rejected by clang-3.2:
// rejected by gcc-4.6.3:
template<typename T>
struct Foo
{
void run(T&) {}
void run(T const&) {}
};

int main()
{
Foo<const int> f;
}

Frank
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: keyserver x-hkp://pool.sks-keyservers.net

iEYEARECAAYFAk+zPnwACgkQhAOUmAZhnmqKGACdHRQG+32tQ2tSmciP7MTXQ0d8
D5UAnijj+tDxyY69BGrZpM13mwoPTT8R
=swna
-----END PGP SIGNATURE-----

Mathias Gaunard

unread,
May 17, 2012, 2:58:37 PM5/17/12
to
On May 8, 8:22 pm, Roman W <bloody_rab...@gazeta.pl> wrote:

> typedef const std::string key;
> [...]
> std::map<key, value, std::less<key>,
> boost::fast_pool_allocator<std::pair<const key, value> > >
> map_with_boost_allocator; // fails

It is not allowed to pass a const-qualified type as the first argument
to the std::map template.
0 new messages