c++ - 与 decltype 一起使用时的函数类型
问题描述
我正在研究decltype
并std::is_same_v
尝试它们的功能。
template<typename T>
void func(T t){}
template<typename T>
using f = decltype(func<T>);
template<typename T>
using ff = decltype((func<T>));
template<typename T>
using fff = void(*)(T);
template<typename T, typename U, typename Z>
void test(T t, U u, Z z){
std::cout << __PRETTY_FUNCTION__ << std::endl;
std::cout << std::boolalpha
<< std::is_same_v<T, U> << " "
<< std::is_same_v<U, Z> << " "
<< std::is_same_v<Z, T>;
}
int main()
{
f<int> f1; // 1
ff<int> ff1 = func<int>; // 2
fff<int> fff1 = func<int>;
test(f1, ff1, fff1);
return 0;
}
输出:
void test(T, U, Z) [with T = void (*)(int); U = void (*)(int); Z = void (*)(int)]
true true true
在编辑时,我错误地删除了参数并运行了代码。演示链接
template<typename T, typename U, typename Z>
void test(T t, U u) // Z z is missing
{ // nothing changed in the body }
no matching function for call to 'test(void (&)(int), void (&)(int), void (*&)(int))'
36 | test(f1, ff1, fff1);
| ^
它看起来像是Z
不同的类型,但std::is_same_v<U, Z>
给出了true
. 而且我认为ff
并且f
会根据decltype
cpprefernce中的不同类型
请注意,如果对象的名称带有括号,则将其视为普通的左值表达式,因此 decltype(x) 和 decltype((x)) 通常是不同的类型。
- 当我尝试初始化时,
f f1 = func<int>;
我收到警告和错误。
warning: declaration of 'void f1(int)' has 'extern' and is initialized
32 | f<int> f1 =func<int>;
| ^~
<source>:32:16: error: function 'void f1(int)' is initialized like a variable
32 | f<int> f1 =func<int>;
| ^~~~~~~~~
- 当我不初始化时
ff ff1;
,我收到一条错误消息
error: 'ff1' declared as reference but not initialized
33 | ff<int> ff1 ;
| ^~~
据我所知,我得到了引用类型,但decltype((func<T>))
由于.std::is_same_v
true
test
Apparenlty,std::is_same_v
告诉所有三个是相同的,但他们是不同的。我是 C++ 的初学者,我无法理解发生了什么。
解决方案
您的代码类似于:
f<int> f1; // `void f1(int);`, function declaration
ff<int> ff1 = func<int>; // `void (&ff1)(int) = func<int>;`, reference to function
fff<int> fff1 = func<int>; // `void (*fff1)(int) = &func<int>;` pointer to function,
// decay of `func<int>` to pointer
作为 C 数组,您不能按值传递函数;它们衰减为指针。
所以
test(f1, ff1, fff1); // test(&f1, &ff1, fff1);
在测试内部,所有参数都有void (*)(int)
类型。
推荐阅读
- api - 我如何显示 API 结果
- c# - INotifyPropertyChanged 与 2 个窗口
- corda - java corda模板构建问题
- java - Move a point toward the mouse
- java - 在 Windows 上为 android 构建 iotivity 时出错
- javascript - 根据 id 将数组中的 n 个对象合并为一个数组
- wordpress - ERR_CONNECTION_TIMED_OUT 托管在 GCP 上的 Wordpress 网站
- php - 试图在隐藏的类型上保存随机并得到错误
- c++ - 如何影响由 new 分配的内存并强制转换为数组 od 类对象?
- excel - Excel 条件格式 - 如果上面的未合并单元格符合条件,则最小值