allocator_traits<Alloc>::select_on_container_copy_construction rejected when used with a parameter pack
struct S
{
template<class... Allocator>
S(const Allocator&...) {}
};
template<class... Allocators>
void foo(const Allocators&... as)
{
S s(std::allocator_traits<Allocators>::select_on_container_copy_construction(as)...); // rejected
// OK:
//S s({std::allocator_traits<Allocators>::select_on_container_copy_construction(as)...});
}
Here's the godbolt link: https://godbolt.org/z/Pear79vvr
I did try to create an example without using allocator_traits but couldn't get it to fail.
Note that there doesn't seem to be a problem in the non-variadic case
@llvm/issue-subscribers-clang-frontend
It does look like clang is getting this wrong but not sure what is going on here CC @royjacobson
I find out that the code below is accepted by clang:
#include <memory>
struct S
{
template<class... Allocator>
S(const Allocator&...) {}
};
template<class... Allocators>
void foo(const Allocators&... as)
{
// before
// S s(std::allocator_traits<Allocators>::select_on_container_copy_construction(as)...); // rejected
// after
S s((std::allocator_traits<Allocators>::select_on_container_copy_construction(as))...);
// OK:
//S s({std::allocator_traits<Allocators>::select_on_container_copy_construction(as)...});
}
godbolt: https://godbolt.org/z/1vWrr5c9z
Yes, that's similar to the 'OK' code I provided. It seems that putting some extra brackets of either type around some part of this solves the problem. The thing which got me thinking that this might help was the error message:
missing 'typename' prior to dependent type name 'std::allocator_traits
select_on_container_copy_construction isn't a type, it's a function. However, it seems implementations tend to return a dependent type. I wondered if this was somehow confusing clang and whether brackets would help. It seems they do help but I don't understand why. I've tried cooking up various examples of my own that don't involve allocator_traits but without luck!
A simpler repro
template<class _Tp>
struct Foo {
static _Tp foo1(_Tp tp) { return tp; }
};
template<class _Tp>
static _Tp foo2(_Tp tp) {
return tp;
}
struct Bar {
template<class... _Tp>
Bar(_Tp... tp) {}
};
template<class... _Tp>
void Baz(_Tp... tp)
{
Bar bar(Foo<_Tp>::foo1(tp)...);
// OK
// Bar bar1({Foo<_Tp>::foo1(tp)...});
// Bar bar2((Foo<_Tp>::foo1(tp))...);
// Bar bar3(foo2(tp)...);
}
godbolt: https://godbolt.org/z/5v5rvrKvb
Also, Bar bar(Foo<_Tp>().foo1(tp)...); will be accepted.
Based on this, I'm wondering if this has something to do with static member function called via class name?
Ah, I think I know why I wasn't able to repro with a simpler case: I'd accidentally swapped in braced init:
https://godbolt.org/z/zPjq3hfsP
Interesting, and in some sense even more weirdly, getting rid of the static in my example - without making any other changes - doesn't change the error message!
Better still, I can completely comment out thing and it still fails with the same error! It's sort of like clang is trying to interpret
s(baz<Allocators>::thing(as)...);
as a function call and assuming that baz<Allocators>::thing is a type.
https://godbolt.org/z/4zcqP97Yr
By looking into dumped ast, I find out that Bar bar2(Foo<_Tp>::foo1(tp)...); is parsed as FuncDecl instead of VarDecl.
template<class _Tp>
struct Foo {
static _Tp foo1(_Tp tp) { return tp; }
};
template<class _Tp>
static _Tp foo2(_Tp tp) {
return tp;
}
struct Bar {
template<class... _Tp>
Bar(_Tp... tp) {}
};
template<class... _Tp>
void Baz(_Tp... tp)
{
Bar bar1((Foo<_Tp>::foo1(tp))...);
Bar bar2(Foo<_Tp>::foo1(tp)...);
// OK
// Bar bar1({Foo<_Tp>::foo1(tp)...});
// Bar bar2((Foo<_Tp>::foo1(tp))...);
// Bar bar3(foo2(tp)...);
}
TranslationUnitDecl 0x5608466902c8 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x560846690b50 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x560846690890 '__int128'
|-TypedefDecl 0x560846690bc8 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x5608466908b0 'unsigned __int128'
|-TypedefDecl 0x560846690f80 <<invalid sloc>> <invalid sloc> implicit __NSConstantString '__NSConstantString_tag'
| `-RecordType 0x560846690cd0 '__NSConstantString_tag'
| `-CXXRecord 0x560846690c28 '__NSConstantString_tag'
|-TypedefDecl 0x560846691028 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
| `-PointerType 0x560846690fe0 'char *'
| `-BuiltinType 0x560846690370 'char'
|-TypedefDecl 0x5608466dd0e0 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list '__va_list_tag[1]'
| `-ConstantArrayType 0x5608466dd080 '__va_list_tag[1]' 1
| `-RecordType 0x560846691130 '__va_list_tag'
| `-CXXRecord 0x560846691088 '__va_list_tag'
|-ClassTemplateDecl 0x5608466dd2b8 <<source>:1:1, line:4:1> line:2:8 Foo
| |-TemplateTypeParmDecl 0x5608466dd140 <line:1:10, col:16> col:16 referenced class depth 0 index 0 _Tp
| `-CXXRecordDecl 0x5608466dd208 <line:2:1, line:4:1> line:2:8 struct Foo definition
| |-DefinitionData empty aggregate standard_layout trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor can_const_default_init
| | |-DefaultConstructor exists trivial constexpr needs_implicit defaulted_is_constexpr
| | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
| | |-MoveConstructor exists simple trivial needs_implicit
| | |-CopyAssignment simple trivial has_const_param needs_implicit implicit_has_const_param
| | |-MoveAssignment exists simple trivial needs_implicit
| | `-Destructor simple irrelevant trivial constexpr needs_implicit
| |-CXXRecordDecl 0x5608466dd528 <col:1, col:8> col:8 implicit struct Foo
| `-CXXMethodDecl 0x5608466dd720 <line:3:5, col:42> col:16 foo1 '_Tp (_Tp)' static implicit-inline
| |-ParmVarDecl 0x5608466dd5e8 <col:21, col:25> col:25 referenced tp '_Tp'
| `-CompoundStmt 0x5608466dd848 <col:29, col:42>
| `-ReturnStmt 0x5608466dd838 <col:31, col:38>
| `-ImplicitCastExpr 0x5608466dd820 <col:38> '_Tp' xvalue <NoOp>
| `-DeclRefExpr 0x5608466dd800 <col:38> '_Tp' lvalue ParmVar 0x5608466dd5e8 'tp' '_Tp'
|-FunctionTemplateDecl 0x5608466ddab0 <line:6:1, line:9:1> line:7:12 foo2
| |-TemplateTypeParmDecl 0x5608466dd868 <line:6:10, col:16> col:16 referenced class depth 0 index 0 _Tp
| `-FunctionDecl 0x5608466dda00 <line:7:1, line:9:1> line:7:12 foo2 '_Tp (_Tp)' static
| |-ParmVarDecl 0x5608466dd918 <col:17, col:21> col:21 referenced tp '_Tp'
| `-CompoundStmt 0x5608466ddc38 <col:25, line:9:1>
| `-ReturnStmt 0x5608466ddc28 <line:8:5, col:12>
| `-ImplicitCastExpr 0x5608466ddc10 <col:12> '_Tp' xvalue <NoOp>
| `-DeclRefExpr 0x5608466ddbf0 <col:12> '_Tp' lvalue ParmVar 0x5608466dd918 'tp' '_Tp'
|-CXXRecordDecl 0x5608466ddc58 <line:11:1, line:14:1> line:11:8 referenced struct Bar definition
| |-DefinitionData pass_in_registers empty standard_layout trivially_copyable has_user_declared_ctor can_const_default_init
| | |-DefaultConstructor exists non_trivial user_provided defaulted_is_constexpr
| | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
| | |-MoveConstructor exists simple trivial needs_implicit
| | |-CopyAssignment simple trivial has_const_param needs_implicit implicit_has_const_param
| | |-MoveAssignment exists simple trivial needs_implicit
| | `-Destructor simple irrelevant trivial constexpr needs_implicit
| |-CXXRecordDecl 0x5608466ddd90 <col:1, col:8> col:8 implicit referenced struct Bar
| `-FunctionTemplateDecl 0x5608466fb578 <line:12:5, line:13:21> col:5 Bar
| |-TemplateTypeParmDecl 0x5608466dde40 <line:12:14, col:23> col:23 referenced class depth 0 index 0 ... _Tp
| `-CXXConstructorDecl 0x5608466fb4c0 <line:13:5, col:21> col:5 Bar 'void (_Tp...)' implicit-inline
| |-ParmVarDecl 0x5608466fb368 <col:9, col:16> col:16 tp '_Tp...' pack
| `-CompoundStmt 0x5608466fb608 <col:20, col:21>
`-FunctionTemplateDecl 0x5608466fb8a0 <line:16:1, line:26:1> line:17:6 Baz
|-TemplateTypeParmDecl 0x5608466fb638 <line:16:10, col:19> col:19 referenced class depth 0 index 0 ... _Tp
`-FunctionDecl 0x5608466fb7f0 <line:17:1, line:26:1> line:17:6 Baz 'void (_Tp...)'
|-ParmVarDecl 0x5608466fb710 <col:10, col:17> col:17 referenced tp '_Tp...' pack
`-CompoundStmt 0x5608466fc080 <line:18:1, line:26:1>
|-DeclStmt 0x5608466fbc48 <line:19:5, col:38>
| `-VarDecl 0x5608466fb9d8 <col:5, col:37> col:9 bar1 'Bar':'Bar' callinit
| `-ParenListExpr 0x5608466fbc28 <col:13, col:37> 'NULL TYPE'
| `-PackExpansionExpr 0x5608466fbbe8 <col:14, col:34> '<dependent type>'
| `-ParenExpr 0x5608466fbbc8 <col:14, col:33> '<dependent type>'
| `-CallExpr 0x5608466fbba0 <col:15, col:32> '<dependent type>'
| |-DependentScopeDeclRefExpr 0x5608466fbb48 <col:15, col:25> '<dependent type>' lvalue
| `-DeclRefExpr 0x5608466fbb80 <col:30> '_Tp' lvalue ParmVar 0x5608466fb710 'tp' '_Tp...'
`-DeclStmt 0x5608466fc068 <line:20:5, col:36>
`-FunctionDecl 0x5608466fbfa0 <col:5, col:35> col:9 bar2 'Bar (Foo<_Tp>::foo1...)'
`-ParmVarDecl 0x5608466fbea0 <col:14, col:31> col:29 tp 'Foo<_Tp>::foo1...' pack
<source>:20:14: error: missing 'typename' prior to dependent type name 'Foo<_Tp>::foo1'
Bar bar2(Foo<_Tp>::foo1(tp)...);
^~~~~~~~~~~~~~
typename
<source>:20:32: error: '...' must immediately precede declared identifier
Bar bar2(Foo<_Tp>::foo1(tp)...);
^~~
...
2 errors generated.
Compiler returned: 1
Damn, so that's a most vexing parse error?
Is Clang actually correct on this?
I realized when I was going to sleep this might be the most vexing parse since the parenthesis seem to disambiguate it, I have to think more on who is correct here.
@royjacobson I think based on temp.variadic p8 and the example in temp.variadic p11:
[Example [7](https://eel.is/c++draft/temp.variadic#example-7):
template<class... T> struct X : T... { };
template<class... T> void f(T... values) {
X<T...> x(values...);
}
template void f<>(); // OK, X<> has no base classes
// x is a variable of type X<> that is value-initialized
— end example]
that clang is not correct.