decltype(*& fun)很奇怪? [英] decltype(*&fun) is strange?

查看:127
本文介绍了decltype(*& fun)很奇怪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

#include <type_traits>
#include <stdio.h>

void f() { printf("foo\n"); }

int main()
{
  printf("%d %d %d\n",
    std::is_same<decltype(*&f),decltype(f)>::value,
    std::is_function<decltype(*&f)>::value,
    std::is_function<decltype(f)>::value);
  (*&f)();
  return 0;
}

产生

0 0 1
foo

.1和4.7.0。

任何人都可以向我解释这个问题吗?

Can anyone explain this to me?

推荐答案

重要的是注意 decltype 有两个含义:它可以用于查找实体的声明类型(因此它的名称),或者可以用于检查表达式。我在这里使用实体,而不是指的标准的任何术语,但简单地说,它可以是一个变量,一个函数,或者(在我看来足够奇怪)成员访问。检查表达式时返回的类型通常与表达式本身的类型没有什么不同,因此:

It's important to note that decltype has two meanings: it can be used to find the declared type (hence its name) of an entity, or it can be used to inspect an expression. I'm using entity loosely here and am not referring to any term of the Standard but to put it simply it could be a variable, a function, or (strangely enough in my opinion) a member access. The type that is returned when an expression is examined is more often than not different from the type of the expression itself, thus:

int i;
void foo();
struct { int i; } t;

static_assert( std::is_same<decltype( i ),     int>::value,       "" );
static_assert( std::is_same<decltype( foo ),   void()>::value,    "" );
static_assert( std::is_same<decltype( t.i ),   int>::value,       "" );

static_assert( std::is_same<decltype( (i) ),   int&>::value,      "" );
static_assert( std::is_same<decltype( (foo) ), void(&)()>::value, "" );
static_assert( std::is_same<decltype( (t.i) ), int&>::value,      "" );

注意这对函数是如何工作的,因此在你的情况下 decltype *& f) decltype((f))相同,而不是 decltype(f)

Note how this works for functions, and hence that in your case decltype(*&f) is the same as decltype( (f) ), not decltype(f).

这篇关于decltype(*&amp; fun)很奇怪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆