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

时间:2019-07-04 04:36:57

标签: c++ templates

我正在尝试构建一个函数模板,例如util::caller,以将存储在std::vector<T>中的元素应用于接受这些元素作为参数的函数。例如,我有一个函数int func(int a, int b, int c)和一个整数std::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之类的东西的定义在本文的后半部分。


当我像func那样调用util::caller(func, args)时,编译器会报告意外错误,但是当我像func那样调用util::caller(func, args, BuildIndices<3>())时,一切都很好。请注意,对于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

c铛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

2 个答案:

答案 0 :(得分:4)

考虑使用两个参数来调用它:

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名称与通常为宏名称保留的名称完全匹配。
  • 是的,编译器消息都很烂。

答案 1 :(得分:4)

我认为这不是错误。编译器没有像您期望的那样推论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>());
}