C++中出现意外的编译错误:将默认值传递给函数参数

Unexpected compile error in C++: passing default value to function parameter

本文关键字:值传 默认值 默认 参数 函数 意外 错误 编译 C++      更新时间:2023-10-16

我正在尝试构建一个函数模板,比如util::caller,将存储在std::vector<T>中的元素应用于接受这些元素作为参数的函数。例如,我有一个函数int func(int a, int b, int c)和一个intstd::vector<int> args = {1, 2, 3}向量,函数调用可能类似于以下代码片段。

int func(int a, int b, int c) {
return a + b + c;
}
int main() {
std::vector<int> args = {1, 2, 3};
util::caller(func, args);
return 0;
}

util::caller的实现几乎完成,其签名如下所示:

template <typename FuncType,
typename VecType,
size_t... I,
typename Traits = function_traits<FuncType>,
typename ReturnT = typename Traits::result_type>
ReturnT caller(FuncType& func,
VecType& args,
indices<I...> placeholder = BuildIndices<Traits::arity>());

东西的定义,如function_traitsBuildIndices,在这篇文章的后面部分。


当我像util::caller(func, args)一样调用func时,编译器会报告意外错误,但是当我像util::caller(func, args, BuildIndices<3>())一样调用func时一切都很好。请注意,在int func(int, int, int)的情况下,Traits::arity等于3UL。也就是说,util::caller的两种召唤是一样的!

这让我很困惑,我不确定这是否是编译器的错误。(GCC,CLANG,ICC,MSVC都将报告该意外错误。谁能解释一下?任何线索或提示将不胜感激。


MWE可以在 https://gcc.godbolt.org/z/JwHk6_或以下位置找到:

#include <iostream>
#include <utility>
#include <vector>
namespace util {
template <typename ReturnType, typename... Args>
struct function_traits_defs {
static constexpr size_t arity = sizeof...(Args);
using result_type = ReturnType;
template <size_t i>
struct arg {
using type = typename std::tuple_element<i, std::tuple<Args...>>::type;
};
};
template <typename T>
struct function_traits_impl;
template <typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(Args...)>
: function_traits_defs<ReturnType, Args...> {};
template <typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(*)(Args...)>
: function_traits_defs<ReturnType, Args...> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...)>
: function_traits_defs<ReturnType, Args...> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) const>
: function_traits_defs<ReturnType, Args...> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) const&>
: function_traits_defs<ReturnType, Args...> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) const&&>
: function_traits_defs<ReturnType, Args...> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) volatile>
: function_traits_defs<ReturnType, Args...> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) volatile&>
: function_traits_defs<ReturnType, Args...> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) volatile&&>
: function_traits_defs<ReturnType, Args...> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) const volatile>
: function_traits_defs<ReturnType, Args...> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) const volatile&>
: function_traits_defs<ReturnType, Args...> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits_impl<ReturnType(ClassType::*)(Args...) const volatile&&>
: function_traits_defs<ReturnType, Args...> {};
template <typename T, typename V = void>
struct function_traits
: function_traits_impl<T> {};
template <typename T>
struct function_traits<T, decltype((void)&T::operator())>
: function_traits_impl<decltype(&T::operator())> {};
template <size_t... Indices>
struct indices {
using next = indices<Indices..., sizeof...(Indices)>;
};
template <size_t N>
struct build_indices {
using type = typename build_indices<N - 1>::type::next;
};
template <>
struct build_indices<0> {
using type = indices<>;
};
template <size_t N>
using BuildIndices = typename build_indices<N>::type;
template <typename FuncType,
typename VecType,
size_t... I,
typename Traits = function_traits<FuncType>,
typename ReturnT = typename Traits::result_type>
ReturnT caller(FuncType& func,
VecType& args,
indices<I...> placeholder = BuildIndices<Traits::arity>()) {
return func(args[I]...);
}
template <typename FuncType>
static constexpr size_t arity(FuncType& func) {
return function_traits<FuncType>::arity;
}
}  // namespace util
int func(int a, int b, int c) {
return a + b + c;
}
int main() {
std::vector<int> args = {1, 2, 3};
int j = util::caller(func, args);  // reports error
// works fine for the following calling
// int j = util::caller(func, args, util::BuildIndices<3>());
// int j = util::caller(func, args, util::BuildIndices<util::arity(func)>());
// int j = util::caller(func, args, util::BuildIndices<util::function_traits<decltype(func)>::arity>());
std::cout << j << std::endl;
return 0;
}

编译器错误报告:

海湾合作委员会 9.1:

<source>: In function 'ReturnT util::caller(FuncType&, VecType&, util::indices<I ...>) [with FuncType = int(int, int, int); VecType = std::vector<int>; long unsigned int ...I = {}; Traits = util::function_traits<int(int, int, int), void>; ReturnT = int]':
<source>:116:34: error: could not convert 'util::BuildIndices<3>()' from 'indices<#'nontype_argument_pack' not supported by dump_expr#<expression error>>' to 'indices<#'nontype_argument_pack' not supported by dump_expr#<expression error>>'
116 |   int j = util::caller(func, args);  // reports error
|                                  ^
|                                  |
|                                  indices<#'nontype_argument_pack' not supported by dump_expr#<expression error>>
<source>:116:34: note:   when instantiating default argument for call to 'ReturnT util::caller(FuncType&, VecType&, util::indices<I ...>) [with FuncType = int(int, int, int); VecType = std::vector<int>; long unsigned int ...I = {}; Traits = util::function_traits<int(int, int, int), void>; ReturnT = int]'
<source>: In function 'int main()':
<source>:116:34: error: could not convert 'util::BuildIndices<3>()' from 'indices<#'nontype_argument_pack' not supported by dump_expr#<expression error>>' to 'indices<#'nontype_argument_pack' not supported by dump_expr#<expression error>>'
Compiler returned: 1

叮当 8.0.0:

<source>:99:26: error: no viable conversion from 'indices<0UL aka 0, 1UL aka 1, sizeof...(Indices) aka 2>' to 'indices<(no argument), (no argument), (no argument)>'
indices<I...> placeholder = BuildIndices<Traits::arity>()) {
^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:116:11: note: in instantiation of default function argument expression for 'caller<int (int, int, int), std::vector<int, std::allocator<int> >, util::function_traits<int (int, int, int), void>, int>' required here
int j = util::caller(func, args);  // reports error
^
<source>:78:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'BuildIndices<function_traits<int (int, int, int), void>::arity>' (aka 'indices<0UL, 1UL, sizeof...(Indices)>') to 'const util::indices<> &' for 1st argument
struct indices {
^
<source>:78:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'BuildIndices<function_traits<int (int, int, int), void>::arity>' (aka 'indices<0UL, 1UL, sizeof...(Indices)>') to 'util::indices<> &&' for 1st argument
struct indices {
^
<source>:99:26: note: passing argument to parameter 'placeholder' here
indices<I...> placeholder = BuildIndices<Traits::arity>()) {
^
<source>:100:25: error: too few arguments to function call, expected 3, have 0
return func(args[I]...);
~~~~           ^
<source>:116:17: note: in instantiation of function template specialization 'util::caller<int (int, int, int), std::vector<int, std::allocator<int> >, util::function_traits<int (int, int, int), void>, int>' requested here
int = util::caller(func, args);  // reports error
^
2 errors generated.
Compiler returned: 1

ICC 19.0.1:

<source>(99): error: no suitable user-defined conversion from "util::BuildIndices<3UL>" to "util::indices<>" exists
indices<I...> placeholder = BuildIndices<Traits::arity>()) {
^
detected during instantiation of "ReturnT util::caller(FuncType &, VecType &, util::indices<I...>) [with FuncType=int (int, int, int), VecType=std::vector<int, std::allocator<int>>, I=<>, Traits=util::function_traits<int (int, int, int), void>, ReturnT=int]" at line 116
<source>(100): error #165: too few arguments in function call
return func(args[I]...);
^
detected during instantiation of "ReturnT util::caller(FuncType &, VecType &, util::indices<I...>) [with FuncType=int (int, int, int), VecType=std::vector<int, std::allocator<int>>, I=<>, Traits=util::function_traits<int (int, int, int), void>, ReturnT=int]" at line 116
compilation aborted for <source> (code 2)
Compiler returned: 2

MSVC 19.21:

example.cpp
<source>(99): error C2440: 'default argument': cannot convert from 'util::indices<0,1,2>' to 'util::indices<>'
<source>(99): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Compiler returned: 2

我认为这不是错误。编译器没有按预期推导I,因为它不应该基于默认参数推导模板参数,如 cppreference.com 上非推导上下文的大小写 (4) 中所述。

也就是说,只要您手动重载caller(而不是使用默认参数),就不难使您的代码按预期工作。

template <typename FuncType,
typename VecType,
size_t... I,
typename Traits = function_traits<FuncType>,
typename ReturnT = typename Traits::result_type>
ReturnT caller(FuncType& func,
VecType& args,
indices<I...> placeholder) {
return func(args[I]...);
}
template <typename FuncType, typename VecType>
typename function_traits<FuncType>::result_type caller(
FuncType& func, VecType& args) {
return caller(func, args, BuildIndices<function_traits<FuncType>::arity>());
}

考虑用两个参数调用它:

template <typename FuncType,
typename VecType,
size_t... I,
typename Traits = function_traits<FuncType>,
typename ReturnT = typename Traits::result_type>
ReturnT caller(FuncType& func,
VecType& args,
indices<I...> placeholder = BuildIndices<Traits::arity>());

基本上有两种方法。一种方法显式指定模板参数(至少是前三种)。您使用的另一种方式将确定模板参数留给编译器。编译器从给定的参数中执行此操作。对于前两个参数,这很容易,它们与给定的参数完全匹配。但是,对于第三个模板参数,这不起作用,因为该参数依赖于第三个参数,而第三个参数又取决于第三个模板参数。

建议:你不能用Traits::arity代替I吗?

笔记:

  • 全大写I名称正式匹配通常保留给宏名称的内容。
  • 是的,编译器消息都很糟糕。