smi*_*hax 4 c++ g++ decltype c++11
我有:
#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
在g ++ 4.6.1和4.7.0上.
任何人都可以向我解释这个吗?
Luc*_*ton 13
重要的是要注意它decltype有两个含义:它可以用于查找实体的声明类型(因此它的名称),或者它可以用于检查表达式.我在这里松散地使用实体并且不是指标准的任何术语,而是简单地说它可以是变量,函数,或者(在我看来,奇怪的是)成员访问.检查表达式时返回的类型通常与表达式本身的类型不同,因此:
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).
| 归档时间: | 
 | 
| 查看次数: | 255 次 | 
| 最近记录: |