【问题标题】:function type when used with decltype与 decltype 一起使用时的函数类型
【发布时间】:2021-04-24 08:06:15
【问题描述】:

我正在研究 decltypestd::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;
}

link to the demo

输出:

void test(T, U, Z) [with T = void (*)(int); U = void (*)(int); Z = void (*)(int)]
true true true

编辑时我错误地删除了参数并运行了代码。 link to the demo

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&lt;U, Z&gt; 给出了true。而且我认为fff 根据decltype in cpprefernce 将是不同的类型

请注意,如果一个对象的名称被括号括起来,它被视为一个普通的左值表达式,因此 decltype(x) 和 decltype((x)) 通常是不同的类型。


  1. 当我尝试初始化 f f1 = func&lt;int&gt;; 时,我收到一个警告和一个错误。
 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>;
      |                ^~~~~~~~~
  1. 当我不初始化 ff ff1; 时,我收到一条错误消息
error: 'ff1' declared as reference but not initialized
   33 |     ff<int> ff1 ;
      |             ^~~

据我所知,由于decltype((func&lt;T&gt;)),我得到了引用类型,但std::is_same_vtest 中给出了true

显然,std::is_same_v 告诉这三个都是相同的,但它们是不同的。我是 C++ 的初学者,我无法理解发生了什么。

【问题讨论】:

  • 不是一个完整的答案,所以作为评论发布。您得到的诊断显示类型不匹配 before the values decayed。请注意these decayed types 与第一个编译示例中的输出相匹配。
  • @PatrickRoberts 谢谢。从问题ff 是一个引用类型函数指针,f 是一个函数指针(不确定)。是不是总是像数组在传递给函数时衰减为指针一样衰减?
  • Neither ff nor f are initially pointer types (though fff is)f 是值类型,ff 是引用类型,但是当传递给 test() 时,两者都会衰减为指针。
  • @PatrickRoberts 非常感谢。现在它更有意义了。我应该把std::is_same_v 放在主要本身。 std::is_same_v&lt;void(int), f&lt;int&gt;&gt; -> true...std::is_same_v&lt;void(&amp;)(int), ff&lt;int&gt;&gt; -> true...std::is_same_v&lt;void(*)(int), fff&lt;int&gt;&gt; -> true。我希望我的所有类型都正确。请考虑添加答案。我会接受的。再次感谢您的时间和耐心。
  • 我不明白f f1 = func&lt;int&gt;;给出的错误是什么意思?

标签: c++ decltype is-same


【解决方案1】:

您的代码类似于:

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)类型。

【讨论】:

  • 谢谢。这个警告信息是什么意思? declaration of 'void f1(int)' has 'extern' and is initialized?
  • 你不能初始化一个函数 int f1(int) = /*..*/;
猜你喜欢
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
  • 2014-10-08
  • 2014-04-13
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多