decltype(*&fun)很奇怪吗?
decltype(*&fun) is strange?
我有:
#include <type_traits>
#include <stdio.h>
void f() { printf("foon"); }
int main()
{
printf("%d %d %dn",
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。
有人能向我解释一下吗?
需要注意的是,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)
。
相关文章:
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- C++decltype和圆括号-为什么
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 如何从"decltype()"获取函数参数的数量<funtion>?
- Confusion: decltype vs std::function
- decltype(1, t) 应该是 l 值引用吗?(编译器不同意)
- 是否可以在不使用 decltype 的情况下推断先前定义的 extern 变量的类型
- 无效 f(int) 的模板和 decltype
- c++ 11 带有 decltype 的尾随返回类型无法按预期工作
- 我可以使用 decltype() 或其他东西通过指针获取真实类型吗?
- 为什么"fun(i)"被推导出为"fun<int&>"而不是"fun<int>",因为"i"是"int"的类型而不是参考?
- decltype:使用指针访问类的静态成员
- decltype() 不适用于正在编译的类模板
- 在部分模板专用化中使用 decltype
- 非静态成员函数的 decltype 格式不正确吗?
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 使用具有默认参数的函数模板进行 decltype 会使结果混乱(一个有趣的问题或 gcc 的错误)
- "decltype(&ordenary_func)"和"decltype"(ordenary_func)之间的区别
- 指向具有 decltype 的函数的指针向量
- decltype(*&fun)很奇怪吗?