预先定义一个可变函数指针参数

预先定义一个可变函数指针参数

本文介绍了预先定义一个可变函数指针参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数 foo ,它接受一个可变函数指针作为其参数。

I have a function foo that takes a variadic function pointer as its argument.

使用using在函数声明之前定义参数的类型。

I would like to use "using" to define the argument's type prior to the function declaration.

template <typename ... vARGS>
using TFuncType = void(*)(vARGS ... V_args);

template <typename ... vARGS>
void foo(TFuncType<vARGS ...> funcptr) {}

void bar(int i) {}

int main() {
  foo(&bar); // This line fails to compile.
}

这不编译。错误(通过clang使用c ++ 1z)是:

This doesn't compile. The error (via clang using c++1z) is:

/make/proj/test/variadic-funcparam-deduce2.cpp:39:5: error: no matching function for call to 'foo'
foo(&bar);
^~~
/make/proj/test/variadic-funcparam-deduce2.cpp:33:36: note: candidate template ignored: substitution failure [with vARGS = int]
template <typename ... vARGS> void foo(TFuncType<vARGS ...> funcptr) {}

替换失败?

如果我在foo()中明确写入类型,我可以成功编译:

I can successfully compile if I explicitly write the type inside foo():

template <typename ... vARGS>
void foo(void(*funcptr)(vARGS ... V_args)) {}

但我不能得到初始(使用)版本工作,即使明确指定模板参数,并使用预先转换的 TFuncType< int> 即:

But I cannot get the initial ("using") version to work even when explicitly specifying the template parameters, and using a pre-casted TFuncType<int> for the argument, i.e.:

int main() {
  TF_call<int> fptr = &bar; // This line is OK.
  foo<int>(fptr);
}

有人知道这里有什么吗?

Does anyone know what's up here?

使用typedef'd(using)可变参数和/或我缺少的函数指针有什么奇怪吗?

Is there something strange about using typedef'd ("using") variadics and/or function pointers that I'm missing?

推荐答案

我相信这可能与我从复制的以下文本相关来自14.5.7 [temp.alias]段落2中的C ++标准:

I believe this may be related to the following text which I copied from this answer that itself takes from the C++ standard in 14.5.7 [temp.alias] paragraph 2:

如果我要解释这一点,那意味着GCC接受代码实际上是不合格的。

If I'm interpreting that right, it means that GCC accepting the code is actually non-conforming.

这篇关于预先定义一个可变函数指针参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:43