【发布时间】:2019-07-04 04:36:57
【问题描述】:
我正在尝试构建一个函数模板,比如util::caller,以将存储在std::vector<T> 中的元素应用于接受这些元素作为参数的函数。比如我有一个函数int func(int a, int b, int c)和一个int向量std::vector<int> args = {1, 2, 3},函数调用可能像下面的代码sn-p。
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_traits和BuildIndices等东西的定义在本文后面部分。
当我像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;
}
编译器错误报告:
gcc 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
clang 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
【问题讨论】: