Justin is correct. Running GCC through a debugger leads to these lines of code:
cp_parser_lookup_name(cp_parser*, tree_node*, tag_types, bool, bool, bool, tree_node**, unsigned int) () at ../../gcc/cp/parser.c:24665
24665 {
(gdb)
24667 tree object_type = parser->context->object_type;
(gdb)
24670 if (ambiguous_decls)
(gdb)
24665 {
(gdb)
24667 tree object_type = parser->context->object_type;
(gdb)
24670 if (ambiguous_decls)
(gdb)
24676 parser->context->object_type = NULL_TREE;
...
(gdb) list 24670
24665 {
24666 tree decl;
24667 tree object_type = parser->context->object_type;
24668
24669 /* Assume that the lookup will be unambiguous. */
24670 if (ambiguous_decls)
24671 *ambiguous_decls = NULL_TREE;
24672
24673 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24674 no longer valid. Note that if we are parsing tentatively, and
And this is the actual code that emits the diagnostic:
6914 complain);
(gdb)
test.cpp:9:24: error: too many arguments to function ‘void bar(Args&& ...) [with Args = {}]’
bar(1, 2.0, &change);
^
test.cpp:2:6: note: declared here
void bar(Args&&...) {}
...
(gdb) list 6914
6909 /* All other function calls. */
6910 postfix_expression
6911 = finish_call_expr (postfix_expression, &args,
6912 /*disallow_virtual=*/false,
6913 koenig_p,
6914 complain);
6915
6916 if (close_paren_loc != UNKNOWN_LOCATION)
6917 {
6918 location_t combined_loc = make_location (token->location,
Skipping through a bunch of stuff (as it would make this answer unnecessarily long), the actual error occurs during overload resolution:
(gdb)
add_candidates (fns=0x7fffeffb0940, first_arg=first_arg@entry=0x0, args=args@entry=0x7fffeff9baf0, return_type=return_type@entry=0x0, explicit_targs=0x0,
template_only=false, conversion_path=0x0, access_path=0x0, flags=1, candidates=0x7fffffffd320, complain=3) at ../../gcc/cp/call.c:5302
5302 for (; fns; fns = OVL_NEXT (fns))
(gdb)
5365 }
(gdb)
perform_overload_resolution (complain=3, any_viable_p=<synthetic pointer>, candidates=0x7fffffffd320, args=0x7fffeff9baf0, fn=<optimized out>)
at ../../gcc/cp/call.c:4036
4036 *candidates = splice_viable (*candidates, false, any_viable_p);
(gdb)
build_new_function_call(tree_node*, vec<tree_node*, va_gc, vl_embed>**, bool, int) () at ../../gcc/cp/call.c:4111
4111 complain);
(gdb)
4115 if (complain & tf_error)
(gdb)
4119 if (!any_viable_p && candidates && ! candidates->next
(gdb)
4120 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
(gdb)
4121 return cp_build_function_call_vec (candidates->fn, args, complain);
The error occurs in convert_arguments
:
(gdb) list 3611
3606 allocated = make_tree_vector ();
3607 params = &allocated;
3608 }
3609
3610 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3611 complain);
3612 if (nargs < 0)
3613 return error_mark_node;
3614
3615 argarray = (*params)->address ();
Finally, the diagnostic is emitted in error_num_args
because if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
is false.
&change
is ill-formed, the compilers treat it as "not a value" and do weird things. It might lead them to instantiate the template asbar<int, double>
because the "not a value" isn't a type so it isn't inserted into the template argument list, which then means that the function is being called with too many arguments. In the case of GCC, it looks like the "not a value" makes the entire type-list empty (with Args = {}
) – Justin Jun 7 '17 at 18:02