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

Strange derived allocator behaviour

9 views
Skip to first unread message

Bonita Montero

unread,
Jun 6, 2021, 1:22:05 PM6/6/21
to
Consider the following code:

#include <vector>
#include <memory>

using namespace std;

template<typename Alloc>
void f( Alloc const &alloc )
{
using IAlloc = typename allocator_traits<Alloc>::template
rebind_alloc<int>;
vector<int, IAlloc> viA( IAlloc( alloc ) ); // doesn't work
vector<int, IAlloc> viB( IAlloc( const_cast<Alloc const &>(alloc) ) );
};


int main()
{
f( allocator<char>() );
}

Why does the first vector-definition not work (at least with MSVC and
clang 11).

Bonita Montero

unread,
Jun 6, 2021, 1:24:44 PM6/6/21
to
g++, even an older version, does compile it.

Bonita Montero

unread,
Jun 6, 2021, 1:29:32 PM6/6/21
to
> Why does the first vector-definition not work (at least with MSVC and
> clang 11).

Ok, found it myself: it seems to be a function-definition.
But why is it parsed as a function-definition although IAlloc( alloc )
isn't a valid parameter-definition ?

Paavo Helde

unread,
Jun 6, 2021, 1:59:41 PM6/6/21
to
IAlloc(alloc) is parsed as 'IAlloc alloc' and is a valid parameter
definition. Here is a simplified version of your line:

vector<int, IAlloc> viA(int(alloc));

which is also parsed as a function declaration:

vector<int, IAlloc> viA(int alloc);

To get it parsed as a variable definition add some parens:

vector<int, IAlloc> viA((IAlloc(alloc))); // does work

I agree these parsing rules are obnoxious and should be changed somehow.


0 new messages