简体   繁体   English

是否可以在C ++中使用函数返回类型作为参数来声明另一个函数?

[英]Is it possible to use function return type as an argument in declaration of another function in C++?

I want something like this: 我想要这样的东西:

std::tuple<int, bool, double> MyFunction_1 (void);

void MyFunction_2 (decltype (MyFunction_1) &params);

Obviously, in this example a code pointer to function would be passed. 显然,在这个例子中,将传递一个代码指向函数的指针。

I want to have the equivalent of this: 我想要相当于这个:

void MyFunction_2 (std::tuple<int, bool, double>  &params);

Is it possible to do so? 有可能这样做吗?

decltype (MyFunction_1) will give you the type of MyFunction_1 (ie the function type std::tuple<int, bool, double> () ), you need to emulate a function calling 1 (via adding () ) to get the return type (ie std::tuple<int, bool, double> ), eg decltype (MyFunction_1)将为您提供MyFunction_1的类型(即函数类型std::tuple<int, bool, double> () ),您需要模拟调用 1 的函数 (通过adds () )来获取返回类型(例如, std::tuple<int, bool, double>

void MyFunction_2 (decltype (MyFunction_1()) &params);
//                                       ^^

1 The expression is evaluated at compile-time, the function won't be called at run-time actually. 1表达式在编译时计算,实际上不会在运行时调用该函数。

The type of MyFunction_1 is not std::tuple<int, bool, double> - informally you can think of it as a function pointer. MyFunction_1类型不是std::tuple<int, bool, double> - 非正式地,您可以将其视为函数指针。 In fact &MyFunction_1 decays to itself and is certainly a pointer. 事实上, &MyFunction_1 衰弱了自己, 当然是一个指针。

So decltype(MyFunction_1) is not what you want. 所以decltype(MyFunction_1)不是你想要的。

The solution is to write decltype(MyFunction_1()) . 解决方案是编写decltype(MyFunction_1()) The type of MyFunction_1() is std::tuple<int, bool, double> . MyFunction_1()类型 std::tuple<int, bool, double> Note that this doesn't actually call the function; 请注意,这实际上并不调用该函数; it's rather like sizeof in that respect. 在这方面,它更像是sizeof

using RetType = std::invoke_result<decltype(f)>::type;

玩它: https//gcc.godbolt.org/z/AE7lj_

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++函数模板:参数类型和返回类型必须使用&吗? - C++ function template: Must use & for argument type and return type? 是否可以在函数指针声明中使用`auto`关键字作为返回类型进行初始化? - Is it possible to use the `auto` keyword as a return type in a function pointer declaration with initialization? 在C ++中将一个函数的返回值用作另一个函数的参数 - using the return value of one function as an argument in another function in c++ 可以在静态类函数[C ++]上使用&#39;using&#39;声明吗? - Is it possible to use 'using' declaration on the static class function [C++]? C++ function 指针在声明和参数 - C++ function pointer at declaration and argument C ++使用函数参数类型进行模板参数推导 - C++ use function argument type for template argument deduction 是否可以将 STL multimap value_type function 与多参数 C++ 构造函数一起使用 - Is it possible to use the STL multimap value_type function with a multiple argument C++ constructor 如何在C ++中推导出函数参数类型? - How is possible to deduce function argument type in C++? 是否可以声明将自身用作C ++参数的函数类型? - Is it possible to declare a function type that uses itself as an argument in C++? 在C ++模板函数中,我可以返回一个解除引用的参数类型吗? - In a C++ template function can I return a dereferenced argument type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM