#include <type_traits>
auto call(const auto& f) -> typename std::result_of<decltype(f)()>::type
{
  return f();
}
int main()
{
  return call([] { return 0; });
}
Neither gcc-4.9.2 and gcc-5.0.0 compil!!!!
原因: result_of needs a call expression from which it will deduce the return type,
const auto& f中的f会被初始化为函数签名原型,而非函数引用.
ok version:
template<typename F>
auto call(F const& f) -> typename std::result_of<decltype(f)()>::type
//                    or typename std::result_of<F()>::type
{
  return f();
}
或者
template<typename F>
auto call(F const& f) -> decltype(f())
{
  return f();
}
Anyway, on gcc 4.5, result_of is implemented in terms of decltype:
template<typename _Signature>
  class result_of;
template<typename _Functor, typename... _ArgTypes>
  struct result_of<_Functor(_ArgTypes...)>
  {
    typedef decltype( std::declval<_Functor>()(std::declval<_ArgTypes>()...) )  type;
  };
  
  
template <class F, class R = result_of_t<F()>>
R call(F& f) { return f(); }
int answer() { return 42; }
call(answer); // error
使用:result_of_t<F&()>
/**************************正确的方式************************************************************/ 
#include <iostream>
#include <type_traits>
double f(int i)
{
        return i+0.1;
}
struct F
{
        public:
        double operator ()(int i) { return i+0.1; }
};
int
main(int, char**)
{
        std::result_of<F(int)>::type x;     
        std::result_of<f(int)>::type x;  //错误的用法
//typename std::result_of<decltype(&f)(int)>::type x; //正确的方式1
        //typename std::result_of<decltype((f))(int)>::type x; //正确的方式2, decltype((f))会返回一个引用
        x = 0.1;
        std::cerr << x << std::endl;
}
struct AA {};
int foo(AA x)
{
    return 0;
}
template<typename T>
void Test(const T& t)
{
    decltype(std::declval<T>()(std::declval<AA>())) i1 = 1;
    typename std::result_of<decltype(&t)(AA)>::type i2 = 2; //正确的方式1

typename std::result_of<decltype(t)(AA)>::type i2 = 2; //正确的方式2

    typename std::result_of<const T &(AA)>::type i3 = 2; //正确的方式3
}
int main()
{
    Test(foo);
    return 0;
}

g++ tpl.cpp -std=c++11 -g -fno-elide-constructors -O0

std result_of的更多相关文章

  1. 【error】no type named ‘type’ in ‘class std::result_of<void

    Q: std::thread fs_module(fs_process, prob_orig, fb_sz, line_num, probp, plabel, std::ref(confidence_ ...

  2. /usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void

    /usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std ...

  3. C++11:使用 auto/decltype/result_of使代码可读易维护

    C++11 终于加入了自动类型推导.以前,我们不得不使用Boost的相关组件来实现,现在,我们可以使用"原生态"的自动类型推导了! C++引入自动的类型推导,并不是在向动态语言(强 ...

  4. C++笔记--std::相关

    std::packaged_task https://www.cnblogs.com/haippy/p/3279565.html https://en.cppreference.com/w/cpp/t ...

  5. C++ std::thread

    std::thread Defined in header class thread The class thread represents a single thread of execution. ...

  6. 第30课 线程同步(std::condition_variable)

    一. 条件变量 (一)条件变量概述 多线程访问一个共享资源(或称临界区),不仅需要用互斥锁实现独享访问避免并发错误,在获得互斥锁进入临界区后,还需检查特定条件是否成立.当某个线程修改测试条件后,将通知 ...

  7. std::invoke_result的实现详解

    目录 目录 前言 invoke_result 标准库中的invoke_result 我的实现 后记 前言 本篇博文将详细介绍一下libstdc++中std::invoke_result的实现过程,由于 ...

  8. C++11的简单线程池代码阅读

    这是一个简单的C++11实现的线程池,代码很简单. 原理就是管理一个任务队列和一个工作线程队列. 工作线程不断的从任务队列取任务,然后执行.如果没有任务就等待新任务的到来.添加新任务的时候先添加到任务 ...

  9. c++新特性与boost

    <Boost程序库探秘——深度解析C++准标准库>之试读 前一阵子还看到一篇文章,说C#要重蹈C++的覆辙,这里说的C++的覆辙是什么呢?是指C++语言过于臃肿的功能特性,导致学习人员的流 ...

随机推荐

  1. 基于TF-IDF值的汉语语义消歧算法

    RT,学校课题需要233,没了 话说,窝直接做个链接的集合好了,方便以后查找 特征值提取之 -- TF-IDF值的简单介绍 汉语语义消歧之 -- 句子相似度 汉语语义消歧之 -- 词义消歧简介 c++ ...

  2. iframe自适应方法

    ifram高度自适应,两种方法 方法一:父页面获取子页面高度,改变父页面高度以下代码加到父页面 <script language="javascript"> //获取i ...

  3. UIActionSheet的使用

    UIActionSheet是在iOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件. 为了快速完成这例子,我们打开Xcode 4.3.2, 先建立一个single view applicatio ...

  4. TreeSet和Comparator 对TreeSet排序

    使用TreeSet和Comparator,编写TreeSetTestInner类,要求对TreeSet中的元素"HashSet"."ArrayList".&qu ...

  5. 计算机网络(1)-----网络层IP协议概述

    网络层(Network Layer) 概念 网络层是OSI参考模型中的第三层,介于传输层和数据链路层之间,它在数据链路层提供的两个相邻端点之间的数据帧的传送功能上,进一步管理网络中的数据通信,将数据设 ...

  6. 通过cpu SN码注册软件类

    using System; using System.Management; namespace SoftReg { internal class SoftReg { #region 变量 ]; // ...

  7. enmo_day_09

    1. 数据库 select name from v$database; : 数据库名称 select db_unique_name from v$database; : 数据库唯一名称 select ...

  8. Android 每次访问网络时,都需要判断是否有网络

    /** * 在执行网络操作之前判断网络是否链接可用 * * @return true 可用 false 不可用 */ private boolean isOnline() { Connectivity ...

  9. 使用查询(SQ01、SQ02、SQ03)创建报表

    查询需求说明 通过Query(SQ01.SQ02.SQ03)实现根据销售组织查询销售订单中的各项信息,包括物料.金额.成本,以及毛利. 其中对销售组织进行权限检查(Authority Check),字 ...

  10. My first Scratch small game

    My first Scratch small game:https://scratch.mit.edu/projects/62700370/ PC or Mac only. Browser & ...